本文介绍了Doctrine 2 - 持续错误 - 警告:spl_object_hash()期望参数1为对象,给定为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在圣经中创建了这样的条目。我正在尝试添加篮子到页面。
这是我的实体:
我的实体:

 <?php 

命名空间App \Entity;
使用Doctrine\Common\Collections\ArrayCollection;
/ **
* @Entity(repositoryClass =App\Repository\Page)
* @Table(name =page)

* /
class Page
{
/ **
* @Id @Column(type =integer,name =p_id)
* @GeneratedValue
* /
private $ p_id;
/ ** @Column(type =string,name =p_title)* /
private $ p_title;
/ ** @Column(type =datetime,name =p_created)* /
private $ p_created_at;
/ ** @Column(type =datetime,name =p_updated_at)* /
private $ p_updated_at;
/ ** @Column(type =text,name =p_abstract)* /
private $ p_abstract;
/ ** @Column(type =text,name =p_fulltext,nullable = false)* /
private $ p_fulltext;
/ ** @Column(type =string,name =p_author,nullable = true)* /
private $ p_author;
/ ** @Column(type =string,name =p_url,nullable = true)* /
private $ p_url;
/ ** @Column(type =string,name =p_meta_title,nullable = true)* /
private $ p_meta_title;
/ ** @Column(type =string,name =p_meta_keywords,nullable = true)* /
private $ p_meta_keywords;
/ ** @Column(type =string,name =p_meta_description,nullable = true)* /
private $ p_meta_description;
/ ** @Column(type =string,name =p_status)* /
private $ p_status;
/ ** @Column(type =string,name =p_weight)* /
private $ p_weight;
/ **
* @ManyToOne(targetEntity =User,inversedBy =pages)
* @JoinColumn(name =p_u_id,referencedColumnName =u_id)
* /
private $ user;

/ **
* @OneToMany(targetEntity =App \Entity\Page\Basket,mappedBy =page,cascade = {persist,remove })
* /
protected $ pageBaskets;


public function __construct()
{
$ this-> pageBaskets = new ArrayCollection();
$ this-> pageMedia = new ArrayCollection();
}
public function __get($ property)
{
return $ this-> property;
}
public function __set($ property,$ value)
{
$ this-> $ property = $ value;
}
public function getPageMedia()
{
return $ this-> pageMedia;
}
public function setUser(\App\Entity\User $ user)
{
$ this-> user = $ user;
}
public function getMedias()
{
return $ this-> pageMedia;
}
public function getPageBaskets()
{
return $ this-> pageBaskets;
}
/ **
*设置页值
* @var数组$ values
* /
public function setPageProperties(array $ values)
{
$ this-> p_updated_at = new \DateTime(now);
$ this-> p_title = $ values ['p_title'];
$ this-> p_abstract = $ values ['p_abstract'];
$ this-> p_meta_title = $ values ['p_meta_title'];
$ this-> p_meta_keywords = $ values ['p_meta_keywords'];
$ this-> p_meta_description = $ values ['p_meta_description'];
$ this-> p_url = $ values ['p_url'];
$ this-> p_fulltext = $ values ['p_abstract'];
$ this-> p_author ='';
$ this-> p_status = 1;

}
public function getSimpleValues()
{
return array(
'p_updated_at'=> $ this-> p_updated_at,
'p_title'=> $ this-> p_title,
'p_abstract'=> $ this-> p_abstract,
'p_meta_title'=> $ this-> p_meta_title,
'p_meta_keywords'=> $ this-> p_meta_keywords,
'p_meta_description'=> $ this-> p_meta_description,
'p_url'=> $ this-> p_url,
'p_fulltext'=> $ this-> p_fulltext,
'p_author'=> $ this-> p_author
);
}
}

?>


<?php
命名空间App \Entity\Page;
使用Doctrine\Common\Collections\ArrayCollection;

/ **
* @Entity
* @Table(name =page_basket)

* /
class Basket
{
/ **
* @Id @Column(type =integer,name =pb_id)
* @GeneratedValue
* /
private $ pb_id;
/ **
* @ManyToOne(targetEntity =Entity\Page)
* @JoinColumn(name =pb_id,referencedColumnName =p_id)
* /
private $ page;


public function __construct()
{

}
public function __get($ property)
{
return $ this-> property;
}
public function __set($ property,$ value)
{
$ this-> $ property = $ value;
}
public function setPage($ page)
{
$ this-> page = $ oszpage;
}
}

?>

但是在这样做的地方在哪里:

  $ page-> getPageBaskets() - > add($ basket); 

我得到:
警告:spl_object_hash()期望参数1为对象,给定null



我做错了什么?

解决方案

在手册中的例子,直到你变得更舒服。删除魔术_ get / _set方法。 D2依赖于截取方法调用来做它的东西。将数组集合作为数组,并构建与实体交互的特定方法。



具体来说:

  public function AddPageBasket($ pageBasket)
{
$ this-> pageBaskets [] = $ pageBasket;
}
...
$ page-> addPageBasket($ pageBasket);

PS。对于您的特殊问题,最有可能的是$ basket不是一个对象。


I create such entries in DOctrine. I'm trying to add basket to page .This is my entities:My Entities:

<?php

namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @Entity(repositoryClass="App\Repository\Page")
 * @Table(name="page")

 */
class Page
{
    /**
     * @Id @Column(type="integer", name="p_id")
     * @GeneratedValue
     */
    private $p_id;
    /** @Column(type="string", name="p_title") */
    private $p_title;
    /** @Column(type="datetime", name="p_created") */
    private $p_created_at;
    /** @Column(type="datetime", name="p_updated_at") */
    private $p_updated_at;
    /** @Column(type="text", name="p_abstract") */
    private $p_abstract;
     /** @Column(type="text", name="p_fulltext", nullable=false) */
    private $p_fulltext;
     /** @Column(type="string", name="p_author", nullable=true) */
    private $p_author;
     /** @Column(type="string", name="p_url",nullable=true) */
    private $p_url;
     /** @Column(type="string", name="p_meta_title",nullable=true) */
    private $p_meta_title;
     /** @Column(type="string", name="p_meta_keywords",nullable=true) */
    private $p_meta_keywords;
     /** @Column(type="string", name="p_meta_description",nullable=true) */
    private $p_meta_description;
      /** @Column(type="string", name="p_status") */
    private $p_status;
    /** @Column(type="string", name="p_weight") */
    private $p_weight;
    /**
     * @ManyToOne(targetEntity="User", inversedBy="pages")
     * @JoinColumn(name="p_u_id", referencedColumnName="u_id")
    */
    private $user;

    /**
     * @OneToMany(targetEntity="App\Entity\Page\Basket", mappedBy="page", cascade={"persist", "remove"})
     */
    protected $pageBaskets;


    public function __construct()
    {
       $this->pageBaskets = new ArrayCollection();
       $this->pageMedia = new ArrayCollection();
    }
    public function __get($property)
    {
        return $this->property;
    }
    public function __set($property,$value)
    {
        $this->$property = $value;
    }
    public function getPageMedia()
    {
        return $this->pageMedia;
    }
    public function setUser(\App\Entity\User $user)
    {
        $this->user = $user;
    }
    public function getMedias()
    {
       return $this->pageMedia;
    }
    public function getPageBaskets()
    {
        return $this->pageBaskets;
    }
    /**
     * Set Page Values
     * @var array $values
     */
    public function setPageProperties(array $values)
    {
        $this->p_updated_at =  new \DateTime("now");
        $this->p_title = $values['p_title'];
        $this->p_abstract = $values['p_abstract'];
        $this->p_meta_title = $values['p_meta_title'];
        $this->p_meta_keywords = $values['p_meta_keywords'];
        $this->p_meta_description = $values['p_meta_description'];
        $this->p_url = $values['p_url'];
        $this->p_fulltext = $values['p_abstract'];
        $this->p_author = '';
        $this->p_status = 1;

    }
    public function getSimpleValues()
    {
        return array(
            'p_updated_at' =>  $this->p_updated_at,
            'p_title' => $this->p_title,
            'p_abstract' =>  $this->p_abstract,
            'p_meta_title' => $this->p_meta_title,
            'p_meta_keywords' => $this->p_meta_keywords,
            'p_meta_description' => $this->p_meta_description,
            'p_url' => $this->p_url,
            'p_fulltext' =>  $this->p_fulltext,
            'p_author' => $this->p_author
        );
    }
}

?>


<?php
namespace App\Entity\Page;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="page_basket")

 */
class Basket
{
    /**
     * @Id @Column(type="integer", name="pb_id")
     * @GeneratedValue
     */
    private $pb_id;
    /**
     * @ManyToOne(targetEntity="Entity\Page", )
     * @JoinColumn(name="pb_id", referencedColumnName="p_id")
    */
    private $page;


    public function __construct()
    {

    }
    public function __get($property)
    {
        return $this->property;
    }
    public function __set($property,$value)
    {
        $this->$property = $value;
    }
    public function setPage($page)
    {
        $this->page = $oszpage;
    }
}

?>

But where are doing such stuff:

 $page->getPageBaskets()->add($basket);

Im getting :Warning: spl_object_hash() expects parameter 1 to be object, null given

What I'm doing wrong ?

解决方案

Best to follow the examples in the manual until you get more comfortable. Drop the magic _get/_set methods. D2 relies on intercepting the method calls to do it's stuff. Treat array collections as arrays and build specific methods for interacting with your entities.

Specifically:

public function addPageBasket($pageBasket)
{
    $this->pageBaskets[] = $pageBasket;
}
...
$page->addPageBasket($pageBasket);

PS. For your particular question, most likely $basket is not an object.

这篇关于Doctrine 2 - 持续错误 - 警告:spl_object_hash()期望参数1为对象,给定为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:07