Phalcon PHP。应该如何查看保存关系的数据一对多?

时间:2016-01-03 16:27:01

标签: php phalcon

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

我有下一个代码来生成checbox:

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

如何查看数据以自动保存此数据?在&#34;自动&#34;我的意思是没有预先添加ID文章manualy。

数据,我试图保存的内容:

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

Model PostsCategories:

public function initialize()
{
    $this->belongsTo("posts_id", "Posts", "id");
    $this->belongsTo("categories_id", "Categories", "id");
}
顺便说一下。在phalcon中有添加/显示多个复选框的标记吗?

2 个答案:

答案 0 :(得分:1)

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

//$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

答案 1 :(得分:0)

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

            $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