Phalcon PHP.应该如何查找保存关系的数据一对多


Phalcon PHP. How should look data for save relation one-to-many?

我想保存文章和类别的关系。我有3张桌子&像这样的模型:https://docs.phalconphp.com/en/latest/reference/models.html#defining-关系

我有下一个代码来生成支票箱:

<div class="form-group">
{% for category in categories %}
  <div class="checkbox">
    <label><input type="checkbox" name="categories[]" value="{{category.id}}">{{category.title}}</label>
  </div>
  {% endfor %}
</div>

需要如何查找数据以自动保存此数据?在"自动"中,我的意思是没有foreach手动添加ID文章。

数据,我试图保存的:

array (size=5)
  'users_id' => string '1' (length=1)
  'title' => string 'Title here' (length=10)
  'content' => string 'content here' (length=12)
  'categories' => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string '4' (length=1)
      2 => string '5' (length=1)
  'slug' => string 'slug-go-here' (length=12)

在我的方法中,我尝试这样保存:

$post = new Posts();
                $success = $post->save($data);
                $array = array_values($this->request->getPost("categories_id"));
                $categories = Categories::find(
                    array(
                        'id IN ({ids:array})',
                        'bind' => array(
                            'ids' => $array
                        )
                    )
                );
                $post->setPostsCategories($categories); 
                $post->save();

模型帖子:

public function initialize()
    {
        $this->belongsTo("users_id", "Users", "id");
        $this->hasOne("id", "PostsImages", "posts_id", [
            'foreignKey' => [
                'action' => Relation::ACTION_CASCADE
            ]
        ]);
        $this->hasMany("id", "PostsCategories", "posts_id", [
            'foreignKey' => [
                'action' => Relation::ACTION_CASCADE
            ]
        ]);
    }

型号类别:

public function initialize()
    {
        $this->hasMany("id", "PostsCategories", "categories_id");
    }

模型发布类别:

public function initialize()
{
    $this->belongsTo("posts_id", "Posts", "id");
    $this->belongsTo("categories_id", "Categories", "id");
}

对了。在phalcon中有任何用于添加/显示多个复选框的标签吗?

在控制器中的某个方法中,您必须执行以下操作:假设您已经从以下表单中获取了数据:

//$id retrieved from POST
//$categoriesIds = $_POST['categories'];

您需要创建实体,绑定并保存它们:

$user = User::find('id', $id);
$categories = Categories::query->inWhere('id', $categoriesIds);
$user->setCategories($categories); //I`m guessing here
$user->save();// if you defined the relationships correctly this should work

最后我创建了一个工作代码:)

            $data = $this->request->getPost();
            // New entity of Post
            $post = new Posts();
            // Creating new entities of PostsCategories and setting categories id
            foreach ($data["categories_ids"] as $k => $id) {
                $posts_cats[$k] = new PostsCategories();
                $posts_cats[$k]->categories_id = $id;
            }
            $post->PostsCategories = $posts_cats; // Set array of PostsCategories objects in related to Posts model "PostsCategories"
            $success = $post->save($data); // The save