Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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
}


83.0k

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);

??



83.0k

Count too:

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


5.3k
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.... ?



83.0k

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

Ok, many thanks !