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

时间:2017-02-25 22:13:45

标签: php zend-framework frameworks zend-framework2

我在做什么

我正在使用带有教义的zend框架2。如果我得到的Zfcuser user_id实例不等于我路线中的参数,我希望我的控制器重定向。

我的控制器操作

public function profileAction(){

    $postId = (int) $this->params()->fromRoute('id');
    //$authService = $this->zfcUserIdentity()->getAuthService();
    $authService = $this->zfcUserAuthentication()->getAuthService();
    $user = $this->zfcUserAuthentication()->getIdentity()->getId();

    $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');     
    $postManager = $this->getServiceLocator()->get('postManager');  

    if ($user !== $postId) {
        return $this->redirect()->toRoute('welcome', array(
                    'controller' => 'company',
                    'action' => 'index'
        ));
    }

    // Create the form.
    $form = new PostForm();

    // Check whether this post is a POST request.
    if ($this->getRequest()->isPost()) {

        // Get POST data.
        $data = $this->params()->fromPost();

        // Fill form with data.
        $form->setData($data);
        if ($form->isValid()) {

            // Get validated form data.
            $data = $form->getData();

            // Use post manager service to add new comment to post.
            $postManager->addPostToUser(
                    $user, $data['fullname'], $data['bank'], $data['accountno'], $data['accounttype'], $data['phonenumber'], $data['tags'], $data['status']);

            // Redirect the user again to "view" page.
            return $this->redirect()->toRoute('company/default', array('controller' => 'post', 'action' => 'view', 'id' => $postId));
        }
    }

    // Render the view template.
    return new ViewModel(array(
        'authSerice' => $authService,
        'user' => $user,
        'form' => $form,
        'postManager' => $postManager
    ));
}

我的实体类

<?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);
    }
}

我的错误消息

致命错误:未捕获错误:在C:\ xampp \ htdocs \ Company \ module \ Company \ src \ Company \ Entity \ Post.php中调用整数上的成员函数addPost():318堆栈跟踪:#0 C :\ xampp \ htdocs \ Company \ module \ Company \ src \ Company \ Service \ PostManager.php(227):Company \ Entity \ Post-&gt; setUser(1)#1 C:\ xampp \ htdocs \ Company \ module \公司\ src \ Company \ Controller \ PostController.php(214):Company \ Service \ PostManager-&gt; addPostToUser(1,&#39; Godwin Mukoro&#39;,&#39; Zenith Bank&#39;,&#39; ; fdsgdfs&#39;,&#39; fgdhfg&#39;,&#39; 08064404662&#39;,&#39; 10000&#39;,&#39; 2&#39;)#2 C:\ xampp \ htdocs \ Company \ vendor \ zendframework \ zendframework \ library \ Zend \ Mvc \ Controller \ AbstractActionController.php(82):Company \ Controller \ PostController-&gt; profileAction()#3 [内部函数]:Zend \ Mvc \ Controller \ AbstractActionController- &gt; onDispatch(Object(Zend \ Mvc \ MvcEvent))#4 C:\ xampp \ htdocs \ Company \ vendor \ zendframework \ zendframework \ library \ Zend \ EventManager \ EventManager.php(444):call_user_func(Array,Object(Zend) \的mvc \ MvcEvent))#5 C:\ xampp \ htdocs \ Company \ vendor \ zendframework \ zend在第318行的C:\ xampp \ htdocs \ Company \ module \ Company \ src \ Company \ Entity \ Post.php

我的问题

如何解决此错误。感谢

1 个答案:

答案 0 :(得分:1)

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

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