Php 致命错误:未捕获错误:对整数调用成员函数addPost()

Php 致命错误:未捕获错误:对整数调用成员函数addPost(),php,zend-framework,frameworks,zend-framework2,Php,Zend Framework,Frameworks,Zend Framework2,我在做什么 我正在使用zend framework 2和条令。如果我得到的Zfcuser user_id实例不等于路由中的参数,我希望控制器重定向 我的控球动作 公共职能行动(){ 我的实体类 <?php namespace Company\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Company\Entity\Comment; use Com

我在做什么

我正在使用zend framework 2和条令。如果我得到的Zfcuser user_id实例不等于路由中的参数,我希望控制器重定向

我的控球动作

公共职能行动(){

我的实体类

<?php
namespace Company\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Company\Entity\Comment;
use Company\Entity\Tag;
use Company\Entity\User;

/**
 * This class represents a single post in a blog.
 * @ORM\Entity(repositoryClass="\Company\Repository\PostRepository")
 * @ORM\Table(name="post")
 */
class Post 
{
    // Post status constants.
    const STATUS_DRAFT       = 1; // Draft.
    const STATUS_PUBLISHED   = 2; // Published.

    /**
     * @ORM\Id
     * @ORM\Column(name="id")
     * @ORM\GeneratedValue
     */
    protected $id;

    /** 
     * @ORM\Column(name="fullname")  
     */
    protected $fullname;

    /** 
     * @ORM\Column(name="bank")  
     */
    protected $bank;

    protected $posts;

    /** 
     * @ORM\Column(name="accountno")  
     */
    protected $accountno;

    /** 
     * @ORM\Column(name="accounttype")  
     */
    protected $accounttype;

    /** 
     * @ORM\Column(name="phonenumber")  
     */
    protected $phonenumber;

    /** 
     * @ORM\Column(name="status")  
     */
    protected $status;

    /**
     * @ORM\Column(name="datecreated")  
     */
    protected $datecreated;

    /**
     * @ORM\OneToMany(targetEntity="\Company\Entity\Comment", mappedBy="post")
     * @ORM\JoinColumn(name="id", referencedColumnName="post_id")
     */
    protected $comments;

    /**
     * @ORM\OneToMany(targetEntity="\Company\Entity\User", mappedBy="posts")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     */
    protected $user;

    /**
     * @ORM\ManyToMany(targetEntity="\Company\Entity\Tag", inversedBy="posts")
     * @ORM\JoinTable(name="post_tag",
     *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
     *      )
     */
    protected $tags;

    /**
     * Constructor.
     */
    public function __construct() 
    {
        $this->comments = new ArrayCollection();        
        $this->tags = new ArrayCollection();        
        $this->users = new ArrayCollection();            
    }

    /**
     * Returns ID of this post.
     * @return integer
     */
    public function getId() 
    {
        return $this->id;
    }

    /**
     * Sets ID of this post.
     * @param int $id
     */
    public function setId($id) 
    {
        $this->id = $id;
    }

    /**
     * Returns fullname.
     * @return string
     */
    public function getFullname() 
    {
        return $this->fullname;
    }

    /**
     * Sets fullname.
     * @param string $fullname
     */
    public function setFullname($fullname) 
    {
        $this->fullname = $fullname;
    }

    /**
     * Returns accountno.
     * @return string
     */
    public function getAccountno() 
    {
        return $this->accountno;
    }

    /**
     * Sets fullname.
     * @param string $accountno
     */
    public function setAccountno($accountno) 
    {
        $this->accountno = $accountno;
    }

    /**
     * Returns accounttype.
     * @return string
     */
    public function getAccounttype() 
    {
        return $this->accounttype;
    }

    /**
     * Sets fullname.
     * @param string $accounttype
     */
    public function setAccounttype($accounttype) 
    {
        $this->accounttype = $accounttype;
    }

    /**
     * Returns phonenumber.
     * @return string
     */
    public function getPhonenumber() 
    {
        return $this->phonenumber;
    }

    /**
     * Sets fullname.
     * @param string $phonenumber
     */
    public function setPhonenumber($phonenumber) 
    {
        $this->phonenumber = $phonenumber;
    }

    /**
     * Returns status.
     * @return integer
     */
    public function getStatus() 
    {
        return $this->status;
    }

    /**
     * Sets status.
     * @param integer $status
     */
    public function setStatus($status) 
    {
        $this->status = $status;
    }   

    /**
     * Returns post bank.
     */
    public function getBank() 
    {
       return $this->bank; 
    }

    /**
     * Sets post bank.
     * @param type $bank
     */
    public function setBank($bank) 
    {
        $this->bank = $bank;
    }

    /**
     * Returns the date when this post was created.
     * @return string
     */
    public function getDatecreated() 
    {
        return $this->datecreated;
    }

    /**
     * Sets the date when this post was created.
     * @param string $datecreated
     */
    public function setDatecreated($datecreated) 
    {
        $this->datecreated = $datecreated;
    }

    /**
     * Returns comments for this post.
     * @return array
     */
    public function getComments() 
    {
        return $this->comments;
    }

    /**
     * Adds a new comment to this post.
     * @param $comment
     */
    public function addComment($comment) 
    {
        $this->comments[] = $comment;
    }

    /**
     * Returns comments for this post.
     * @return array
     */
    public function getPosts() 
    {
        return $this->posts;
    }

    /**
     * Adds a new comment to this post.
     * @param $post
     */
    public function addPost($post) 
    {
        $this->posts[] = $post;
    }

    /**
     * Returns tags for this post.
     * @return array
     */
    public function getTags() 
    {
        return $this->tags;
    }      

    /**
     * Adds a new tag to this post.
     * @param $tag
     */
    public function addTag($tag) 
    {
        $this->tags[] = $tag;        
    }

    /**
     * Removes association between this post and the given tag.
     * @param type $tag
     */
    public function removeTagAssociation($tag) 
    {
        $this->tags->removeElement($tag);
    }

    /*
     * Returns associated post.
     * @return \Company\Entity\User
     */
    public function getUser() 
    {
        return $this->user;
    }

    /** 
     * Sets associated post.
     * @param \Company\Entity\User $user
     */
    public function setUser($user) 
    {
        $this->user = $user;
        $user->addPost($this);
    }
}

首先,您没有在错误发生的地方发布代码。正如错误消息所述,addPost()方法是在post类中的一个整数上调用的,可能是第318行的模型。如果您发布这个代码片段,那么我们可能会更好地了解出了什么问题

另一方面,阅读错误消息并试着理解它。它说什么?这很简单。您可能是在整数(如id)上调用addPost()方法,而不是在模型本身上调用。
调试愉快!

谢谢@thomas_inckx我已经添加了我的实体addPost()怎么称呼在zfcuser user_id实例上。再次感谢您的timeHi Mukoro,我看到您编辑了您的问题。检查$user变量。您不是在用户模型上,而是在用户id上调用该方法。您所说的一切都是正确的,但我发现仍然很难解决此问题。如何正确调用是我的问题。我已经尝试过了我能弄明白的一切,但问题拒绝了。请给我更多关于如何做出正确改变的提示?非常感谢
<?php
namespace Company\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Company\Entity\Comment;
use Company\Entity\Tag;
use Company\Entity\User;

/**
 * This class represents a single post in a blog.
 * @ORM\Entity(repositoryClass="\Company\Repository\PostRepository")
 * @ORM\Table(name="post")
 */
class Post 
{
    // Post status constants.
    const STATUS_DRAFT       = 1; // Draft.
    const STATUS_PUBLISHED   = 2; // Published.

    /**
     * @ORM\Id
     * @ORM\Column(name="id")
     * @ORM\GeneratedValue
     */
    protected $id;

    /** 
     * @ORM\Column(name="fullname")  
     */
    protected $fullname;

    /** 
     * @ORM\Column(name="bank")  
     */
    protected $bank;

    protected $posts;

    /** 
     * @ORM\Column(name="accountno")  
     */
    protected $accountno;

    /** 
     * @ORM\Column(name="accounttype")  
     */
    protected $accounttype;

    /** 
     * @ORM\Column(name="phonenumber")  
     */
    protected $phonenumber;

    /** 
     * @ORM\Column(name="status")  
     */
    protected $status;

    /**
     * @ORM\Column(name="datecreated")  
     */
    protected $datecreated;

    /**
     * @ORM\OneToMany(targetEntity="\Company\Entity\Comment", mappedBy="post")
     * @ORM\JoinColumn(name="id", referencedColumnName="post_id")
     */
    protected $comments;

    /**
     * @ORM\OneToMany(targetEntity="\Company\Entity\User", mappedBy="posts")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     */
    protected $user;

    /**
     * @ORM\ManyToMany(targetEntity="\Company\Entity\Tag", inversedBy="posts")
     * @ORM\JoinTable(name="post_tag",
     *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
     *      )
     */
    protected $tags;

    /**
     * Constructor.
     */
    public function __construct() 
    {
        $this->comments = new ArrayCollection();        
        $this->tags = new ArrayCollection();        
        $this->users = new ArrayCollection();            
    }

    /**
     * Returns ID of this post.
     * @return integer
     */
    public function getId() 
    {
        return $this->id;
    }

    /**
     * Sets ID of this post.
     * @param int $id
     */
    public function setId($id) 
    {
        $this->id = $id;
    }

    /**
     * Returns fullname.
     * @return string
     */
    public function getFullname() 
    {
        return $this->fullname;
    }

    /**
     * Sets fullname.
     * @param string $fullname
     */
    public function setFullname($fullname) 
    {
        $this->fullname = $fullname;
    }

    /**
     * Returns accountno.
     * @return string
     */
    public function getAccountno() 
    {
        return $this->accountno;
    }

    /**
     * Sets fullname.
     * @param string $accountno
     */
    public function setAccountno($accountno) 
    {
        $this->accountno = $accountno;
    }

    /**
     * Returns accounttype.
     * @return string
     */
    public function getAccounttype() 
    {
        return $this->accounttype;
    }

    /**
     * Sets fullname.
     * @param string $accounttype
     */
    public function setAccounttype($accounttype) 
    {
        $this->accounttype = $accounttype;
    }

    /**
     * Returns phonenumber.
     * @return string
     */
    public function getPhonenumber() 
    {
        return $this->phonenumber;
    }

    /**
     * Sets fullname.
     * @param string $phonenumber
     */
    public function setPhonenumber($phonenumber) 
    {
        $this->phonenumber = $phonenumber;
    }

    /**
     * Returns status.
     * @return integer
     */
    public function getStatus() 
    {
        return $this->status;
    }

    /**
     * Sets status.
     * @param integer $status
     */
    public function setStatus($status) 
    {
        $this->status = $status;
    }   

    /**
     * Returns post bank.
     */
    public function getBank() 
    {
       return $this->bank; 
    }

    /**
     * Sets post bank.
     * @param type $bank
     */
    public function setBank($bank) 
    {
        $this->bank = $bank;
    }

    /**
     * Returns the date when this post was created.
     * @return string
     */
    public function getDatecreated() 
    {
        return $this->datecreated;
    }

    /**
     * Sets the date when this post was created.
     * @param string $datecreated
     */
    public function setDatecreated($datecreated) 
    {
        $this->datecreated = $datecreated;
    }

    /**
     * Returns comments for this post.
     * @return array
     */
    public function getComments() 
    {
        return $this->comments;
    }

    /**
     * Adds a new comment to this post.
     * @param $comment
     */
    public function addComment($comment) 
    {
        $this->comments[] = $comment;
    }

    /**
     * Returns comments for this post.
     * @return array
     */
    public function getPosts() 
    {
        return $this->posts;
    }

    /**
     * Adds a new comment to this post.
     * @param $post
     */
    public function addPost($post) 
    {
        $this->posts[] = $post;
    }

    /**
     * Returns tags for this post.
     * @return array
     */
    public function getTags() 
    {
        return $this->tags;
    }      

    /**
     * Adds a new tag to this post.
     * @param $tag
     */
    public function addTag($tag) 
    {
        $this->tags[] = $tag;        
    }

    /**
     * Removes association between this post and the given tag.
     * @param type $tag
     */
    public function removeTagAssociation($tag) 
    {
        $this->tags->removeElement($tag);
    }

    /*
     * Returns associated post.
     * @return \Company\Entity\User
     */
    public function getUser() 
    {
        return $this->user;
    }

    /** 
     * Sets associated post.
     * @param \Company\Entity\User $user
     */
    public function setUser($user) 
    {
        $this->user = $user;
        $user->addPost($this);
    }
}