We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Help me with model please

I try write some base model, but it's very difficult, ex: \Phalcon\Mvc\Model\Row dont have ->dump() method, to display all data and so on. So my question is: how i can rewrite the code below with phalcon?

$user = Users::first(array( 'with' => 'UsersEmails', 'conditions' => array( 'user_email' => '[email protected]', 'Users.user_id' => 1, ) )); $user->UsersEmails->confirmed = 1; $uesr->save();

Maybe some allready wrote some parent model class, to use phalcon as lithium or yii?

Give something like this a try:

<?php

$user = Users::findFirst(array(
  'conditions' =>
    'user_email = ?1 and '.
    'user_id = ?2',
  'bind' => array(
    1 => '[email protected]',
    2 => 1
  )
));

foreach ($user->getUsersEmails() as $user_email) {
  $user_email->confirmed = 1;
  $user_email->save();
}

Unless user_email is part of UsersEmails model, then try:

<?php

$user = Users::findFirst(array(
  'conditions' =>
    'user_id = ?1',
  'bind' => array(
    1 => 1
  )
));

foreach ($user->getUsersEmails("user_email = '[email protected]'") as $user_email) {
  $user_email->confirmed = 1;
  $user_email->save();
}

Or a really condensed version:

<?php

$user = Users::findFirst(1);

foreach ($user->getUsersEmails("user_email = '[email protected]'") as $user_email) {
  $user_email->confirmed = 1;
  $user_email->save();
}

Or the most condensed version:

<?php

foreach (Users::findFirst(1)->getUsersEmails("user_email = '[email protected]'") as $user_email) {
  $user_email->confirmed = 1;
  $user_email->save();
}

This assumes, of course, that you've set up a hasMany relationship in your Users and a belongsTo relationship in your UsersEmails models.

I haven't tested it because I don't have your exact use case, but, this should either work or be a huge step in the right direction.

Brian, thanks for you atantion. But if i hasnt user_id, i have only user_email from UsersEmails table, and i want find user, can i do this with one query? Not use ..->getUsers()

Is there a reason you object to:

UsersEmails::findByUserEmail('[email protected]')->getUsers();

which is the preferred way to do what you are asking? You could also use PHQL (select * from UsersEmails left join Users) or raw sql in the way you probably already know how.