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

Proper way to check if many to many exists

Hi there,

I have some many to many relations. e.g. Person has Jobs and the Jobs which are assigned to a person.

Now I want to check if a Person already has a job I assign it and run in a constraint error....

Is there a better way than this?:

$personJob = PersonJob::findFirst(array(
    "conditions" => "personID = ?1 AND jobID = ?2",
    "bind" => array(1 => $personID, 2 => $jobID)
));

if($personJob == true)
{
    //person has already the job
}else{
    //jobless => assign the job
}


98.9k

A 'count' would be lighter than a 'findFirst':

$haveJobs = PersonJob::count(array(
    "conditions" => "personID = ?1 AND jobID = ?2",
    "bind" => array(1 => $personID, 2 => $jobID)
));
edited Jun '14

Okey thanks.

this is still so much code...

Is there nothing like $person = Person::findFirst($personID);

if($person->hasJob($jobID);

??



98.9k

Count too:

$person = Person::findFirst($personID); 
if ($person->countJobs($jobID)) {


8.8k
Accepted
answer

Ok i'll us it that way thanks a lot?

By the way: which IDE is the best for phalcon? NetBeans isn't it.... ?



98.9k

I use Atom or Sublime, aren't IDEs in every sense of the word. PHPStorm is a good choice https://www.jetbrains.com/phpstorm/

Ok, many thanks !