首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >致命错误:未知错误:调用整数上的成员函数addPost()

致命错误:未知错误:调用整数上的成员函数addPost()
EN

Stack Overflow用户
提问于 2017-02-25 22:13:45
回答 1查看 1.9K关注 0票数 1

我在做什么

我的工作是zend框架2和理论。如果我得到的Zfcuser user_id实例与我的路由中的参数不相等,我希望控制器重定向。

My Contoller Action

公共函数profileAction() {

代码语言:javascript
运行
复制
    $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
    ));
}

我的实体类

代码语言:javascript
运行
复制
<?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:318堆栈跟踪中整数上的成员函数addPost()的调用:#0 C:\xampp\htdocs\Company\module\Company\src\Company\Service\PostManager.php(227):Company\Entity\Post->setUser(1) #1 C:\xampp\htdocs\Company\module\Company\src\Company\Controller\PostController.php(214):Company\Service\PostManager->addPostToUser(1,'Godwin‘),‘'Zenith’,'fdsgdfs','fgdhfg','08064404662','10000','2') #2 C:\xampp\htdocs\Company\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(82):Company\Controller\PostController->profileAction() #3内部函数: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent)) #4 C:\xampp\htdocs\Company\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(444):call_user_func(Array,对象(Zend\Mvc\MvcEvent) #5 C:\xampp\htdocs\Company\vendor\zendframework\zend在第318行的C:\xampp\htdocs\Company\module\Company\src\Company\Entity\Post.php中

我的问题

如何解决此错误。谢谢

EN

Stack Overflow用户

回答已采纳

发布于 2017-02-25 22:35:12

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

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

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42462198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档