We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Getting data from the database from addons

Development and Programming

vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Is there a simple and safe way to get data from inside an EE add-ons file without having to create a new PHP SQL query to the database?

Example:

Get the member ID from the email address.

I could just do this in PHP, create an SQL query, and pass the email address to the table that matches the ID number for that member table.

Just curious if there is a native API, or model to do this that would be more elegant.

       
Yuri's avatar
Yuri
315 posts
4 years ago
Yuri's avatar Yuri

The recommended way would be to use the Models.

In your case, this would be something like

$member = ee('Model')->get('Member')->filter('email', '[email protected]')->first();
echo $member->member_id;

You can also make use of (legacy) database class

? 2
       
vw000's avatar
vw000
482 posts
4 years ago
vw000's avatar vw000

Many thanks Yuri! 😊

I was actually trying this already (using models), but the documentation does not really have examples for beginners, so I was stuck with this so far while playing around:

$userID = ee('Model')->get('Member')->first();
echo $userid;

Your code is precisely the examples that would be really useful for new comes to EE. I actually searched all search engines. Even StackOverflow and got no results with just searching ee(‘Model’) for some examples on how this is used in EE.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.