Doctrine字段在数据库中具有值,但在映射时为null

时间:2017-08-06 11:22:33

标签: php mysql symfony doctrine-orm doctrine

Doctrine我遇到了这个奇怪的问题。

当我使用find(id)查找实体时,除了id之外,所有字段都为null。我已经仔细检查了数据库中的值,它们不是null。

奇怪的是,当我发出一个本机mysql查询并试图找到doctrine尝试获取的实体时,我得到非Null值,所以可能它是一种获取值问题的学说方式。

这是我的实体:

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

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

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $client_id;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $region_id;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $street;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $city;

    /**
     * @ORM\Column(name="`state`", type="text", nullable=true)
     */
    protected $state;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $zip_code;

    /**
     * @ORM\Column(type="float", precision=10, scale=6, nullable=true)
     */
    protected $latitude;

    /**
     * @ORM\Column(type="float", precision=10, scale=6, nullable=true)
     */
    protected $longitude;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $notes;

    /**
     * @ORM\Column(name="`status`", type="boolean", nullable=true)
     */
    protected $status;

    /**
     * @ORM\Column(type="smallint", nullable=true)
     */
    protected $archive;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created_at;

    /**
     * @ORM\OneToMany(targetEntity="Store", mappedBy="address")
     * @ORM\JoinColumn(name="id", referencedColumnName="address_id")
     */
    protected $Stores;

    /**
     * @ORM\ManyToOne(targetEntity="Client", inversedBy="Addresses")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
     */
    protected $client;

    /**
     * @ORM\OneToMany(targetEntity="Client", mappedBy="PPBAddress")
     * @ORM\JoinColumn(name="id", referencedColumnName="ppb_address_id")
     */
    protected $clientHQ;

    /**
     * @ORM\ManyToOne(targetEntity="Region", inversedBy="Addresses")
     * @ORM\JoinColumn(name="region_id", referencedColumnName="id")
     */
    protected $region;

    public function __construct()
    {
        if (!$this->created_at) {
            $this->created_at = new \DateTime();
        }

        $this->Clients = new ArrayCollection();
        $this->Stores = new ArrayCollection();
    }

    public function __toString()
    {
        return (!$this->street || strlen($this->street) == 0 || !$this->city || !$this->city)
            ? "Address incomplete"
            : sprintf('%s, %s %s %s', $this->street, $this->city, $this->state, $this->zip_code);
    }

     public function __sleep()
    {
         return array('id', 'client_id', 'region_id', 'street', 'city', 'state', 'zip_code', 'latitude', 'longitude', 'notes', 'status', 'archive', 'created_at');
    }
}

2 个答案:

答案 0 :(得分:0)

您在实体类中缺少getter和setter。

php bin/console doctrine:generate:entities *your bundle*

会自动生成它们

答案 1 :(得分:-1)

var list = document.querySelectorAll(".bx li"); // initialize string variable for HTML var html = ''; // iterate over the nodelist using for loop for (var i = 0; i < list.length; i++) { // append the HTML content to the string variable html += list[i].innerHTML; } GM_setClipboard(html); 没有错。事实证明,doctrine是从原生查询中提取的,只选择了address,这就是为什么我得到id的值而其余为null的原因。

这是找到地址的方式:

id
相关问题