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

Get results only related count greater than zero

Hi!

I want to list keywords only have sites.

$this->hasManyToMany(
    "id",
    __NAMESPACE__ . '\SitesKeywords',
    "keyword_id", "site_id",
    __NAMESPACE__ . '\Sites',
    "id",
    array(
        'alias' => 'sites',
        'params' => array(
                        'active' => 'Y'
                    )
    )
);

How can i get results only related count greater than zero. For ex:

$parameters = array('active' => 'Y', 'sites > 0');
$keywords   = Keywords::find($parameters);

Thanks



11.6k

$res = mymodel->relationAlias->toArray(); $count = count($res); switch($count) { case 0: ....



58.1k

$res = mymodel->relationAlias->toArray(); $count = count($res); switch($count) { case 0: ....

I want to use where query instead manual process. Already i'm using with if and else.

Like in all frameworks. You need to join it in query. Use modelsManager or ::query



58.1k

Like in all frameworks. You need to join it in query. Use modelsManager or ::query

Can i use without query like on defining model relationships?

edited May '16

What you mean ? No you can't you need to use JOINS model relationships are only to access relations in model object(SO FIRST YOU NEED TO FIND IT TO ACCESS IT). And you don't want to do it in your case. As i unerstand you want to select Keywords which have sites > 0. So you need to query keywords and join sites where count of sites > 0 and group results by kerwords id.

It's basically just sql, you have PHQL for this. ::query and modelsManager just gives you OOP way to handle this.

But of course you can still achieve some pros from having relations - you don't need to type join condition - phalcon will do it.

You just need to tell phalcon somehow. Hey, join sites when i select keywords, beacause i need to access them WHEN selecting keywords(NOT AFTER which alias is for). It doesn't doing it itself(which is very good way).