Вопрос

I have a has_many relationship between two tables (Users and Posts), and i would like to insert an array into the second one (Posts).

table users :

id
name

table posts :

id
user_id
title

Model User:

protected $_has_many = array(
        'posts' => array('model'=>'post', 'foreign_key' => 'user_id'),
    );

Model Post :

protected $_belongs_to = array(
        'users' => array('model'=>'user', 'foreign_key' => 'user_id')
    );

Controller :

$posts = ORM::factory('post');
$posts->user = $user->id;
$posts->title = Array("Title1","Title2","Title3","Title4","Title5"); //That's what i want to do.
$posts->save();

Any idea ?

Это было полезно?

Решение

I got it !

The ORM doesn't support multi insert so i used DB.

$posts = DB::insert('posts',array("user_id","title");
foreach ($array as $title)
{
    $posts->values(array($user->id,$title));
}
$posts->execute();

maybe this can help someone :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top