Doctrine 2 category_id在设置ManyToOne关系时总是为空 [英] Doctrine 2 category_id always NULL when set ManyToOne relationship

查看:103
本文介绍了Doctrine 2 category_id在设置ManyToOne关系时总是为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我插入新条目时,如果我设置ManyToOne关系类别,我将无法填写categoryId字段为什么?

When I insert new entry if I set ManyToOne relationship "Category" i will not able to fill "categoryId" field why ?

这是与关系的实体:

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Item
 *
 * @ORM\Table(name="item")
 * @ORM\Entity
 */
class Item extends Base
{
    /**
     * @ORM\ManyToOne(targetEntity="Category")
     */
    private $category;


    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    public $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=40, nullable=false)
     */
    public $name;

    /**
     * @var integer
     *
     * @ORM\Column(name="category_id", type="integer", nullable=true)
     */
    public $categoryId;

}

这是我为生成getter和setter而设的Base类并允许$ entry-> name ='yo'而不是$ entry-> setName('yo');

This is a Base class I made for generate getter and setter and allow $entry->name = 'yo' instead of $entry->setName('yo');

<?php

namespace Application\Entity;

class Base
{
    public function __call($method, $args) {
        if (preg_match('#^get#i', $method)) {
            $property = str_replace('get', '', $method);
            $property = strtolower($property);
            return $this->$property;
        }

        if (preg_match('#^set#i', $method)) {
            $property = str_replace('set', '', $method);
            $property = strtolower($property);
            $this->$property = $args[0];
        }
    }

    public function fromArray(array $array = array()) {
        foreach ($array as $key => $value) {
            $this->$key = $value; 
        }
    }
}

这是我如何保存新的项目:

This is how i save new item :

$item = new \Application\Entity\Item();
$item->name = 'Computer';
$item->categoryId = '12';
$this->em->persist($item);
$this->em->flush();

有什么问题?

推荐答案

你做错了!使用Doctrine,您不能使用category_id列(和类似的),而是使用关系。原则将负责列。

You are doing it wrong! With Doctrine, you don't work with category_id column (and alike) but with relations. Doctrine will take care of columns.

您必须阅读文档,但正确的方式将是:

You will have to read the docs but correct way would be:

$category = new Category() ;
$category->setName("Food") ;

$item = new Item() ;
$item->setName("Pizza") ;
$item->setCategory($category) ;

$em->persist($item) ;
$em->flush() ;

这是100%正确的做事方式,你甚至不需要坚持新创建类(教义会为你做这个)。但是手动尝试设置category_id列是完全错误的做法。

This is 100% correct way of doing things, you don't even need to persist newly create Category (Doctrine will do that for you). But manually trying to set category_id column is completely wrong way of doing things.

另外一个:
不要尝试使Doctrine2的ActiveRecord。当我从D1切换到D2时,我正在考虑做同样的事情,但最后,认为这是浪费时间。看起来你正在努力制作自己的框架;不要那样做。反而学习Symfony2这不容易,但值得的时候。

One more: Don't try to make ActiveRecord of Doctrine2. When I was switching from D1 to D2, I was thinking of doing the same thing but at the end, figured it was waste of time. It looks like you are trying to make your own framework; do NOT do that. Learn Symfony2 instead; it is not easy but it is worth the time.

这篇关于Doctrine 2 category_id在设置ManyToOne关系时总是为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆