多对多插入原则

2023-08-18php开发问题
1

本文介绍了多对多插入原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一些问题.我正在研究并尝试所有建议但没有一个工作.

i have some promblem. i was research and try all suggest but no one work.

我最终得到:

传递给 EntityUser::addCategories() 的参数 1 必须是 EntityCategory 的实例,给定字符串,

我有很多关系,用户、用户类别和类别

i have manytomany relationship, user, user_category, and category

用户

<?php

namespace Entity;

use DoctrineCommonCollectionsArrayCollection;

use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;

/**
 * @Entity
 * @Table(name="user")
 */
class User
{

    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Column(type="string", length=255, unique=true, nullable=false)
     */
    public $name;


    /**
     * @ManyToMany(targetEntity="EntityCategory", inversedBy="user")
     * @ORMJoinTable(name="user_category")
     */
    public $categories;

        public function __construct() {
            $this->category = new DoctrineCommonCollectionsArrayCollection();
    }

    public function getCategories()
    {
        return $this->categories;
    }

    public function addCategories(Category $category = null)
    {
        $this->categories = $category;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
    public function getName()
    {
        return $this->name;
    }

}

类别

<?php

namespace Entity;

use DoctrineCommonCollectionsArrayCollection;

use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;

/**
 * @Entity
 * @Table(name="category")
 */
class Category
{

    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @Column(type="string", length=255, unique=true, nullable=false)
     */
    public $name;

    /**
     * @ManyToMany(targetEntity="EntityUser", mappedBy="category")
     */
    public $user;


    public function __construct() {
        $this->user = new DoctrineCommonCollectionsArrayCollection();
    }

    public function getUser()
    {
        return $this->user;
    }

    public function addUser(User $user = null)
    {
        $user->addCategory($this);
        $this->user = $user;
    }


    public function setName($name)
    {
        $this->name = $name;
    }
    public function getName()
    {
    return $this->name;
    }
}

插入函数

        // check existence object in database
        $res = $this->em->find('EntityUser', $this->input->post('id'));

        if($res){
            $data = $this->em->find('EntityUser', $this->input->post('id'));
        }else{
            // create a new User object
            $data = new EntityUser;                
        }


        $data->setName($this->input->post('name'));
        $data->addCategories($this->input->post('category'));


        // save the data object to the database
        $this->em->persist($data);

        $this->em->flush();

get 一切顺利,但我对开始工作感到很困惑.

Everything goes fine on get but i'm so confuse for set to work.

感谢您的帮助.对不起我的英语.

thanks for your help. sorry for my english.

推荐答案

你有很多错误(注意语法):

You have lot of errors (pay attention to grammar):

代替

public $categories;
public function __construct() {
    $this->category = new DoctrineCommonCollectionsArrayCollection();
}

应该是:

protected $categories;

public function __construct() {
    $this->categories = new DoctrineCommonCollectionsArrayCollection();
}

代替:

public $user;
public function __construct() {
    $this->user = new DoctrineCommonCollectionsArrayCollection();
}

使用

protected $users;
public function __construct() {
    $this->users = new DoctrineCommonCollectionsArrayCollection();
}

代替

public function addCategories(Category $category = null)
{
    $this->categories = $category;
}

一定是

public function addCategory(Category $category = null)
{
    $this->categories->add($category);
}

public function removeCategory(Category $category)
{
    $this->categories->removeElement($category) ;
}
public function setCategories($categories)
{
    $this->categories = categories;
}

双方的逻辑相同.我不知道 CI 是如何工作的,但 Symfony 会自动找到 addSomething/removeSomething 方法.即使 CI 不支持该功能,您仍应按上述方式更改代码.

Same logic on both sides. I don't know how CI works but Symfony will automatically find addSomething/removeSomething methods. Even if CI doesn't support that feature, you should still change your code as above.

这篇关于多对多插入原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17