2017-02-25 73 views
1

調用一個成員函數addPost()我在做什麼致命錯誤:未捕獲的錯誤:整數

我與Zend框架2教義工作。我想我的控制器重定向,如果我得到的Zfcuser user_id實例不等於我的路由中的參數。

我的位指示行動

公共函數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); 
    } 
} 

我的錯誤消息

致命錯誤:未捕獲錯誤:調用成員函數addPost()在C:\ xampp \ htdocs \ Company \ module \ Company \ src \ Company \ Entity \ Post.php中的整數中:318堆棧跟蹤:#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 Mukoro','Zenith Bank','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,Object(Zend \ Mvc \ MvcEvent )#5 C:\ xampp \ htdocs \ Company \ vendor \ zendframework \ zend在C:\ xampp \ htdocs \ Company \ Module \ Company \ src \ Company \ Entity \ Post.php上線318

My問題

如何解決此錯誤。謝謝

回答

1

嗯,首先,你沒有發佈錯誤發生的代碼。正如錯誤消息所述,addPost()方法在Post類中的整數上調用,可能是模型的第318行。如果您發佈此代碼段,那麼我們可以更好地瞭解哪裏出了問題。

另一方面,閱讀錯誤信息並嘗試理解它。它說什麼?這非常簡單。你可能在一個整數(比如一個id)上調用addPost()方法,而不是模型本身。 快速調試!

+0

謝謝@thomas_inckx我已經添加了我的實體,如何在zfcuser user_id實例上調用addPost()。再次感謝您的時間 –

+0

嗨Mukoro,我看到你編輯你的問題。檢查你的$ user變量。而不是在用戶模型上,您正在調用用戶標識中的方法。 –

+0

您所說的一切都是正確的,但我仍然很難解決這個問題。如何正確進行此調用是我的問題。我嘗試了所有我能想出來的東西,但問題拒絕了。 請給出更多關於如何進行正確更改的提示嗎?非常感謝 –

相關問題