Setting Variables in the pivot table on many to many

I have the tables Users, Emails, and UserEmails. in the UserEmails pivot table I have a flag for Primary, where would I set that in this scenario when saving?

 // Initiate User Information
        $user = new \Api\Models\User();

        // Add Emails to User
        $email = new  \Api\Models\Email();
        $email->setEmail($_POST['email']);
        $email->setPrimary(1);  // Creates error because this flag is on the pivot table
        $emails[] = $email;
        $user->emails = $emails;
        $user->create();

Thanks



29.9k

Hi,

there is no method setPrimary defined in Phalcon\Mvc\Model so I guess it's one that you'have created by your own ...

What do you want to achiecve exactly ? If it's setting towo composite primary keys the pivot table, it looks like it is not available:

https://github.com/phalcon/cphalcon/issues/1160#issuecomment-23495879

Just a question regarding you app: is it accurate that an email hasMany User ?

edited Dec '16

I just had to switch to trickle down code, the setPrimary() is a getter/setter. I use those to control variable types

 $user = new \Api\Models\User();

// Create Pivot Entry
$userEmail = new \Api\Models\UserEmail();
$userEmail->setPrimary(1);

// Add Emails to User
$email = new  \Api\Models\Email();
$email->setEmail($_POST['email']);

$userEmail->email = $email;
$user->user_email = $userEmail;


29.9k
edited Dec '16

the setPrimary() is a getter/setter

so you use it to set a primary key (wich one) to a given value ? Or I do still do not understand what you're trying to do ... Anyway, if it's something like that, I think it is not implemented see above.

otherwise, ....

$email = new  \Api\Models\Email();
    // you store email data in Email model
    $email->setEmail($_POST['email']);

    // you store it in the pivot model
    $userEmail->email = $email;
    // and then in the User model
    $user->user_email = $userEmail;