0

Anybody knows how to execute this using the famous Cakephp ORM-based 'find' query function?

$sql = "SELECT id FROM users WHERE id IN (SELECT user_id 
                                          FROM classification 
                                          WHERE class_name = '".$classname."')";

What I want to do is to get the id of the user ( from users table ) whenever a class_name is submitted.

As you can see, the user_id is the foreign key of the classifications table.

1 Answer 1

1

If you have User and Classification models with Classification having hasMany relationship to User, you can go with

$result = $this->Classification->find('first', array('conditions' => array(
    'Classification.class_name' => $classname
)));

foreach ($result['Classification']['User'] as $user)
    echo 'User id: ' . $user['id'] . '<br />';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I do have a question.. Can we execute ConnectionManager :: getDataSource('second_db') inside the User model? Because I'm using external connection to another non-cake app. Or do I need to create another fresh model that relates to that application but resides inside CakePHP? Or, am I silly enough to ask this? :)
better to use a model with the specific datasource specified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.