1

我有一个实体,BaseValue作为映射的超类。第二个实体,称为Field,正在映射这个超类。

我可以存储它,并且子类的值BaseValue存储在正确的表中。

但是,如果我尝试阅读它们,则会收到此错误:

执行 'SELECT t0.id AS id_1, t0.iid AS iid_2, t0.lid AS lid_3, t5.fid AS fid_4 FROM fields t0 LEFT JOIN BaseValue t5 ON t5.fid = t0.id WHERE t0.iid = ?带参数 [1]:

SQLSTATE [42S02]:未找到基表或视图:1146 表“testproject.BaseValue”不存在

当然它不存在,因为它没有价值。这些都存储到子实体的表中。

派生实体(映射超类):

<?php

namespace my\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/** @ORM\MappedSuperclass */
class BaseValue
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="Field", inversedBy="value")
     * @ORM\JoinColumn(name="fid", referencedColumnName="id")
     **/
    private $field;

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

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

    public function setField($field){
        $this->field=$field;
    }

    public function getField(){
        return $this->field;
    }
}

其中一个孩子:

<?php

namespace my\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Value
 * @package my\Entity
 *
 * @ORM\Entity
 * @ORM\Table(name="integers")
 */
class Integer extends BaseValue
{
    /**
     * @var integer
     *
     * @ORM\Column(name="value", type="integer", nullable=true)
     */
    protected $value;

    /**
     * @return string
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param string $value
     */
    public function setValue($value)
    {
        $this->value = $value;
    }
}

与其中一个孩子有关系的实体:

<?php
namespace my\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Field
 * @package my\Entity
 *
 * @ORM\Entity
 * @ORM\Table(name="fields")
 */
class Field
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @var
     * @ORM\ManyToOne(targetEntity="Item", inversedBy="fields")
     * @ORM\JoinColumn(name="iid", referencedColumnName="id")
     */
    protected $item;

    /**
     * @var
     * @ORM\ManyToOne(targetEntity="Label", inversedBy="fields")
     * @ORM\JoinColumn(name="lid", referencedColumnName="id")
     */
    protected $label;

    /**
     * @ORM\OneToOne(targetEntity="BaseValue", mappedBy="field", cascade="persist")
     **/
    private $value;

    protected $temp;

    public function __construct($label=null, $value=null){
        $this->setLabel($label);
        $this->setValue($value);
    }

    public function setItem(Item $item = null){
        $this->item = $item;
    }

    public function getItem(){
        return $this->item;
    }

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

    /**
     * @return string
     */
    public function getValue()
    {
        return $this->value->getValue();
    }

    /**
     * @param string $value
     */
    public function setValue($value)
    {
        $sType = gettype($value);
        switch($sType){
            case 'boolean':
                $this->setBooleanValue($value);
                break;
            case 'integer':
                $this->setIntegerValue($value);
                break;
            case 'double':
                $this->setDoubleValue($value);
                break;
            case 'string':
                $this->setStringValue($value);
                break;
            case 'array':
                $this->setArrayValue($value);
                break;
            case 'object':
                $this->setObjectValue($value);
                break;
            case 'resource':
                $this->setResourceValue($value);
                break;
            case 'NULL':
                $this->setNullValue();
                break;
            default:
                break;
        }
    }

    protected function setBooleanValue($value){
        $this->value = new Boolean($value);
        $this->value->setValue($value);
        $this->value->setField($this);
    }

    protected function setIntegerValue($value){
        $this->value = new Integer($value);
        $this->value->setValue($value);
        $this->value->setField($this);
    }

    protected function setDoubleValue($value){
        $this->value = new Double($value);
        $this->value->setValue($value);
        $this->value->setField($this);
    }

    protected function setStringValue($value){
        $this->value = new String($value);
        $this->value->setValue($value);
        $this->value->setField($this);
    }

    protected function setArrayValue($value){
        throw new \Exception ('arrays are currently not working');
    }

    protected function setObjectValue($value){
        throw new \Exception ('objects are currently not working');
    }

    protected function setResourceValue($value){
        throw new \Exception ('resources are currently not working');
    }

    protected function setNullValue(){
    }

    public function setLabel($label){
        if( is_object($label) && 'my\Entity\Label' == get_class($label)){
            $this->label = $label;
            $this->temp=null;
        }else{
            $this->temp = $label;
        }
    }

    public function getLabel(){
        if( $this->label !== null){
            return $this->label;
        } else {
            return $this->temp;
        }
    }
}

控制器,读取:

public function testRead()
{
    /* @var \my\Entity\Item $item  */
    /* @var \my\Entity\Collection $collection  */
    $item = $this->getEntityManager()->getRepository('my\Entity\Item')->findOneBy(array('id'=>'1'));
    $this->sDesktop .= 'Item ID = ' . $item->getId();
    $collection = $item->getCollection();
    $this->sDesktop .= '<br>Collection = ' . $collection->getName();
    $this->sDesktop .= '<br>Directive = ' . $collection->getDirective();
    count($item->getFields());
}   

在这里它崩溃了:

Doctrine\DBAL\Exception\TableNotFoundException

日期:
/var/www/html/myproject/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php :53

Meldung:
执行 'SELECT t0.id AS id_1, t0.iid AS iid_2, t0.lid AS lid_3, t5.fid AS fid_4 FROM pimfields t0 LEFT JOIN BaseValue t5 ON t5.fid = t0.id WHERE t0 时发生异常。 iid = ?' 带参数 [1]:

SQLSTATE [42S02]:未找到基表或视图:1146 表“myproject2.BaseValue”不存在

4

1 回答 1

0

在文档中,您可以阅读:

映射的超类不能是实体,它不是可查询的,并且由映射的超类定义的持久关系必须是单向的(仅具有拥有方)

您在指向 mapped-super-class$value的实体中定义了一个反面。这是不允许的,并且很可能会导致问题。FieldBaseValue

我建议在继续之前阅读有关正确使用该学说的所有文档@MappedSuperClass,因为遵循文档以防止出现问题非常重要。

还建议在开发期间对您的学说模型模式进行一些验证,以确保您的所有映射都是正确的。

于 2015-08-31T11:59:08.927 回答