Insert new data with `manytomany` relationship

Hi Guys, I can't seem to find a good documentation about ORM in phalconPHP, I've been using Doctrine2 for the past couple of months and would like to ask how to insert new data with manytomany relationship in PhalconPHP.

Supposed I have User and Role entity, how would I save a new user data with role in it? Thanks in advance!



83.5k

Let's pretend you have the following models in your application:

class Artists extends Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->hasManyToMany(
            'id',
            'ArtistsSongs',
            'artists_id',
            'songs_id',
            'Songs',
            'id'
        );
    }
}

class ArtistsSongs extends Phalcon\Mvc\Model
{

}

class Songs extends Phalcon\Mvc\Model
{

}

Then you can insert data on the many-to-many relation:

$songs = array()

$song = new Song();
$song->name = 'Get Lucky';
$songs[] = $song;

$song = new Song();
$song->name = 'Instant Crush';
$songs[] = $song;

$artist = new Artists();
$artist->name = 'Daft Punk';
$artist->songs = $songs; //Assign the n-m relation
$artist->save()

Hi, I have a similar question.

I have 2 tables: Users and Friends.

/**
 * Main Users Model
 */

namespace Models\Users;

class Main extends \Models\Main
{
    public function initialize()
    {
        $this->hasManyToMany(
            'id',
            'Models\Users\Friends',
            'user_id',
            'friend_id',
            'Models\Users\Main',
            'id', array('alias' => 'friends')
        );    
    }
}

And ```php /** * User's friends Model */

namespace Models\Users;

class Friends extends \Models\Main { }

`Users` is the main table that contains all users and their data. `Friends` is just a relations table with 2 columns: user_id and friend_id, both uses main `Users` table. As you see in `Users` I'm using hasManyToMany relationship. It works perfect while reading:

php $user = \Models\Users\Main::findFirst(1);

foreach($user->friends as $friend) { echo $friend->name; } ``` But I can't understand how to add new friends for users. I'm trying to update: ```php $new_friends = array();

$user = \Models\Users\Main::findFirst(1);

$newfriends[] = \Models\Users\Main::findFirst(2); $newfriends[] = \Models\Users\Main::findFirst(3);

$user->friends = $new_friends;

$user->save(); ``` And this code is just adding/overwriting (expectedly) a single row:

+---------+-----------+
| user_id | friend_id |
+---------+-----------+
|       1 |         3 |
+---------+-----------+

On saving it's trying to add already existing user: ```php $new_friends = array();

$user = new \Models\Users\Main();

$user->name = 'New user';

$newfriends[] = \Models\Users\Main::findFirst(2); $newfriends[] = \Models\Users\Main::findFirst(3);

$user->friends = $new_friends;

$user->save();

/** * Integrity constraint violation: 1062 Duplicate entry 'User with id 2' for key */ ``` Is it possible to solve this task without third table and additional ids? I've barely used Kohana ORM before, so the solution may be obviously for the community.

Thanks in advance!



2.6k

Recently I update phalcon version to 2.0.0, but many-to-many relation between models can not work well.When I fetch records through this, it returns NULL.