2017-02-25 3 views
1

, was ich tueFatal error: Uncaught Fehler: Rufen Sie auf eine Elementfunktion addPost() auf integer

ich mit einem Zend Framework 2 mit Lehre arbeite. Ich möchte, dass mein Controller umleitet, wenn die Zfcuser-Benutzer-ID, die ich bekommen habe, nicht mit dem Parameter in meiner Route übereinstimmt.

Meine Contoller Aktion

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

Meine Entity Klasse

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

Meine Fehlermeldung

Fatal error: Uncaught Fehler: Rufen Sie auf eine Elementfunktion addPost() auf ganze Zahl in C: \ xampp \ htdocs \ Unternehmen \ Modul \ Unternehmen \ src \ Unternehmen \ Entity \ post.php: 318 Stack-Trace: # 0 C : \ xampp \ htdocs \ Firma \ module \ Firma \ src \ Firma \ Service \ PostManager.php (227): Firma \ Entität \ Post-> setUser (1) # 1 C: \ xampp \ htdocs \ Firma \ module \ Firma \ src \ Firma \ Controller \ PostController.php (214): Firma \ Service \ PostManager-> addPostToUser (1, 'Godwin Mukoro', 'Zenith Bank', 'fdsgdfs', 'fgdhfg', '08064404662', '10000' , '2') # 2 C: \ xampp \ htdocs \ Firma \ Hersteller \ zendframework \ zendframework \ Bibliothek \ Zend \ Mvc \ Controller \ AbstractActionController.php (82): Firma \ Controller \ PostController-> profileAction() # 3 [ interne Funktion]: Zend \ Mvc \ Controllers \ AbstractActionController-> onDispatch (Object (Zend \ Mvc \ MvcEvent)) # 4 C: \ xampp \ htdocs \ Unternehmen \ Anbieter \ ZendFramework \ ZendFramework \ library \ Zend \ Eventmanager \ EventManager.php (444): call_user_func (Array, Objekt (Zend \ Mvc \ MvcEvent) # 5 C: \ xampp \ htdocs \ Firma \ Hersteller \ zendframework \ zend in C: \ xampp \ htdocs \ Firma \ module \ Firma \ src \ Firma \ Firma \ Post.php Zeile 318

Meine Frage

Wie kann ich diesen Fehler beheben. Dank

Antwort

1

Nun, zunächst einmal, Sie haben den Code nicht schreiben, wo der Fehler stattfindet. Wie die Fehlermeldung besagt, wird die addPost() -Methode für eine ganze Zahl in der Post-Klasse, wahrscheinlich das Modell, in Zeile 318 aufgerufen. Wenn Sie dieses Snippet posten, haben wir möglicherweise eine bessere Vorstellung davon, was schief läuft.

Auf der anderen Seite, lesen Sie die Fehlermeldung und versuchen, es zu verstehen. Was sagt es? Es ist ziemlich geradlinig. Wahrscheinlich rufen Sie die Methode addPost() für eine Ganzzahl (wie eine ID) anstatt für das Modell selbst auf. Happy Debugging!

+0

Danke @thomas_inckx Ich habe meine Einheit hinzugefügt Wie rufe ich die addPost() auf der zfcuser User_id Instanz. Nochmals vielen Dank für Ihre Zeit –

+0

Hallo Mukoro, ich sehe, Sie haben Ihre Frage bearbeitet. Überprüfen Sie Ihre $ user-Variable. Statt im Benutzermodell rufen Sie die Methode für die Benutzer-ID auf. –

+0

Alles, was Sie gesagt haben, ist korrekt, aber ich finde es immer noch schwierig, dies zu lösen. Wie man diesen Anruf richtig macht, ist mein Problem. Ich habe alles versucht, was ich herausfinden konnte, aber das Problem lehne ab. Bitte geben Sie mehr Hinweis auf genau, wie Sie die richtige Änderung vornehmen? Ich danke dir sehr –

Verwandte Themen