在mutil数据库中具有Symfony中的学说的Fetching Related Object为null

时间:2018-12-30 12:49:06

标签: symfony doctrine

config
    config.yml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver: pdo_mysql
                host: '%database_host%'
                port: '%database_port%'
                dbname: '%database_name%'
                user: '%database_user%'
                password: '%database_password%'
                charset: UTF8

            customer:
                driver: pdo_mysql
                host: '%database_host%'
                port: '%database_port%'
                dbname: 'trade'
                user: '%database_user%'
                password: '%database_password%'
                charset: UTF8
    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    AppBundle:  ~
            customer:
                connection: customer
                mappings:
                    AppBundle:
                      dir: EntityTrade


AppBundle\Entity\acclogin
class acclogin
{
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
        /**
         * @ORM\Column(type="string",unique=true)
         * @Assert\NotBlank(message="can't empty!",groups={"registration","login"})
         * @Assert\Length(min=6,max=64,minMessage="invalid 1",maxMessage="invalid 1",groups={"registration","login"})
         */
        private $name;
        /**
         * @ORM\OneToMany(targetEntity="AppBundle\EntityTrade\PayOrder",mappedBy="account")
         */
        private $payorders;

      public function getId()
        {
            return $this->id;
        }

        /**
         * Set name
         *
         * @param string $name
         *
         * @return account_login
         */
        public function setName($name)
        {
            $this->name = $name;

            return $this;
        }

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

//AppBundle\EntityTrade\PayOrder
class PayOrder
{
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
        /**
         * @ORM\Column(type="string")
         * @Assert\NotBlank(message="can't empty!",groups={"payAdd"})
         */
        private $pay_id;

        /**
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\acclogin",inversedBy="payorders")
         * @ORM\JoinColumn(name="pay_id",referencedColumnName="name")
         */
        private $account;
      .....
     ....
     ...........here is getter and setter
}

//AppBundle\EntityTrade\PayOrderRepository
class PayOrderRepository extends \Doctrine\ORM\EntityRepository {
        public function get( PayOrder $payOrder ) {
            $pay_no = $payOrder->getPayNo();
            return $this->findOneBy( [ 'pay_no' => $pay_no ] );
        }
}

在此代码“ $ this-> findOneBy(['pay_no'=> $ pay_no]);”处抛出异常“ AppBundle \ Entity \ acclogin上的主键ID缺少值”;     为什么?我必须在“名称”上添加主键,任何机构都可以帮助我吗?谢谢 你非常!     如果我将@ORM \ JoinColumn(name =“ pay_id”,referencedColumnName =“ name”)更改为@ORM \ JoinColumn(name =“ id”,referencedColumnName =“ id”),我可以使用,但$ PayOrder-> getAccount (),只有id包含数据,acclogin的名称始终为null。

1 个答案:

答案 0 :(得分:0)

Doctrine要求您使用联接列上的主键。

在此处查看JoinColumn()的文档: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_joincolumn

特别是这一行:

  

referencedColumnName :使用的主键标识符的名称   加入这种关系。