1

What I am Doing

I am working with a zend framework 2 with doctrine. I want my controller to redirect if the Zfcuser user_id instance i got is not equal to the parameter in my route.

My Contoller Action

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

My Entity Class

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

My Error Message

Fatal error: Uncaught Error: Call to a member function addPost() on integer in C:\xampp\htdocs\Company\module\Company\src\Company\Entity\Post.php:318 Stack trace: #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 [internal function]: 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 in C:\xampp\htdocs\Company\module\Company\src\Company\Entity\Post.php on line 318

My Question

How do I resolve this error. Thanks

1 Answer 1

2

Well, first of all, you didn't post the code where the error takes place. As the error message states, the addPost() method is called on an integer in the Post class, probably the model, on line 318. If you post this snippet, then we might have a better idea of what goes wrong.

On the other hand, read the error message and try to understand it. What does it say? It's pretty straight forward. You're probably calling the method addPost() on an integer (like an id) instead of on the model itself. Happy debugging!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @thomas_inckx I have added my entity how do I call the addPost() on the zfcuser user_id instance. Once again thanks for your time
Hi Mukoro, I see you edited your question. Check your $user variable. Instead of on the User model, you're calling the method on the user id.
Everything you have said is correct yet I find it still difficult to resolve this. How to make this call correctly is my problem. I have tried everything I could figure out but problem refuse to go. Please could give more hint on exactly on how to make the correct change? Thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.