dp13668681869 2016-02-24 19:12
浏览 112
已采纳

Doctrine + Zend Framework 2:以多对多的关系插入数据数组

I have an application with Zend Framework2 and Doctrine2 as ORM. I have this Entity called User:

namespace Adm\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;

    /**
     * @ORM\Column(type="string")
     */
    protected $email;

    /**
     * @ORM\Column(type="string")
     */
    protected $password;

    /**
     * @ORM\ManyToMany(targetEntity="Module")
     * @ORM\JoinTable(
     *  name="user_module",
     *  joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *  inverseJoinColumns={@ORM\JoinColumn(name="module_id", referencedColumnName="id")}
     * )
     */
    protected $modules;

    public function __construct() {
        $this->modules = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * @return the $id
     */
public function getId() {
    return $this->id;
}

/**
 * @return the $name
 */
public function getName() {
    return $this->name;
}

/**
 * @return the $email
 */
public function getEmail() {
    return $this->email;
}

/**
 * @return the $password
 */
public function getPassword() {
    return $this->password;
}

/**
 * @param field_type $id
 */
public function setId($id) {
    $this->id = $id;
}

/**
 * @param field_type $name
 */
public function setName($name) {
    $this->name = $name;
}

/**
 * @param field_type $email
 */
public function setEmail($email) {
    $this->email = $email;
}

/**
 * @param field_type $password
 */
public function setPassword($password) {
    $this->password = $password;
}

/**
 * Add module
 *
 * @param dm\Entity\Module
 * @return User
 */
public function addModules(Module $modules = null){
    $this->modules[] = $modules;
}

/**
 * Get modules
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getModules(){
    return $this->modules;
}

}

See the modules property is a relation Many to Many with a table called user_modules. And i have the Entity Module as well:

namespace Adm\Entity;

use Doctrine\ORM\Mapping as ORM;
class Module{
/**
 * @ORM\Id
 * @ORM\Column(type="integer");
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string")
 */
private $name;

/**
 * @ORM\Column(type="integer")
 */
private $status;

/**
 * @return the $id
 */
public function getId() {
    return $this->id;
}

/**
 * @return the $name
 */
public function getName() {
    return $this->name;
}

/**
 * @return the $status
 */
public function getStatus() {
    return $this->status;
}

/**
 * @param field_type $id
 */
public function setId($id) {
    $this->id = $id;
}

/**
 * @param field_type $name
 */
public function setName($name) {
    $this->name = $name;
}

/**
 * @param field_type $status
 */
public function setStatus($status) {
    $this->status = $status;
}

}

I receive a array variable containing the Post from a form to insert in a table. Each post element have it's property in Entity, as expected. Together, i have a $module variable which is an array containing id's of the modules. My question is: How do i insert this data in the user_module table? My add function is this:

public function addUser($newUser){
        $user = new User();
        $user->setName($newUser['name']);
        ...
        $this->getEm()->persist($user);
        $this->getEm()->flush();
    }
  • 写回答

2条回答 默认 最新

  • dsbqfrr098575666 2016-02-24 20:57
    关注

    Firstly you need to have cascade={"persist"} as mentioned by @skrilled.

    Then you need to retrieve the module entities from the database. You mentioned you have the id's in the $module variable.

    You need a DQL statement something like this

    $builder = $this->getEntityManager()->createQueryBuilder();
    $builder->select('m')
            ->from('Adm\Entity\Module', 'm')
            ->where('m.id IN (:modules)')
            ->setParameter('modules', $modules);
    $moduleEntities= $builder->getQuery()->getResult(Query::HYDRATE_OBJECT);
    

    and in your User entity you will need

    public function addModules(Array $moduleEntities)
    {
        foreach ($moduleEntities as $module) {
            if ($module instanceof Module) {
                $this->modules[] = $module;
            }
        }
        return $this;
    }
    

    finally in your addUser method you will need to add the array of modules from the above DQL

    public function addUser($newUser, $moduleEntities)
    {
        $user = new User();
        $user->setName($newUser['name']);
        ....
        $user->addModules($moduleEntities);
        $this->getEm()->persist($user);
        $this->getEm()->flush();
    }
    

    I hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.