PhalconPHP how to condition findFirst by join data

Hi everyone!

I have two tables:

AuctionPackage

$this->belongsTo('id', 'AuctionPackageTranslation', 'auction_package_id', NULL);

and another one

AuctionPackageTranslation

$this->belongsTo('auction_package_id', 'AuctionPackage', 'id', NULL);

Now, when I do

$auction_order = AuctionOrder::findFirstByid($id);

I get all the correct data and in Volt I have access to the translation. But, I want to use findFirst instead of findFirstByid and do a condition, if one of the field in the joined language table has a ceratain value.

Like so:

$auction_package = AuctionPackage::findFirst(array(
        "auction_package_translation.lang_id = '{$lang}'",
        "id = {$id}"
    ));

And I get error:

Unknown model or alias 'auction_package_translation' (1), when preparing: SELECT [AuctionPackage]. FROM [AuctionPackage] WHERE auction_package_translation.lang_id = 'de' LIMIT ?1*

Does findFirst not join the tables and findFirstByid ? How can I condition something to joined table?



85.3k

I would not follow this approach, it potentially will allow to inject SQL in your application, the correct one is:

$auction_package = AuctionPackage::findFirst(array(
    "auction_package_translation.lang_id = :lang: AND id = :id:",
    "bind" => array("lang" => $lang, "id" => $id)
));


951
edited Nov '14

But this is wrong..Are you sure?

I would not follow this approach, it potentially will allow to inject SQL in your application, the correct one is:

$auction_package = AuctionPackage::findFirst(array(
  "auction_package_translation.lang_id = :lang: AND id = :id:",
  "bind" => array("lang" => $lang, "id" => $id)
));

I was trying to use this in my project and it did not work.. Does someone know if it is right?

I would not follow this approach, it potentially will allow to inject SQL in your application, the correct one is:

$auction_package = AuctionPackage::findFirst(array(
  "auction_package_translation.lang_id = :lang: AND id = :id:",
  "bind" => array("lang" => $lang, "id" => $id)
));