2
votes

I use TYPO3 7.6 and extbase. I have model "Ticket", "Answer" and "Status"

Ticket n:1 -> Status;
Ticket 1:n -> Answer;
Answer n:1 -> Ticket;
Status 1:n -> Ticket;

In Answer Controller

public function createAction(\Vendor\Extname\Domain\Model\Ticket $ticket, \Vendor\Extname\Domain\Model\Answer $newAnswer)
  {
      //Set tikcet and it's normaly
      $newAnswer->setTicket($ticket);
      // Try to set status and get error 
      $newAnswer->getTicket($ticket)->setStatus($status);
 }

In template Answer/New

<f:form action="create" method="POST" enctype="multipart/form-data" name="newAnswer" controller="Answer" object="{newAnswer}" 
arguments="{ticket:ticket}">

      <f:form.select class="form-control" property="ticket.status" name="status" options="{status}" 
        optionLabelField="title" 
        optionValueField="uid"  />

    </f:form>

and I get

"PHP Catchable Fatal Error: Argument 1 passed to Domain\Model\Ticket::setStatus() must be an instance of Domain\Model\Status, null given"

How can I set value for multiple model objects via one form? Set status for ticket when I create answer.

1
In your example function, what is $status? Where is this set?sven

1 Answers

0
votes

Even this question is quite old:

You can type-hint and assign different object-types as parameter if they share the same interface. Then the type-hint has to be about the interface but not a specific object or another data-type:

namespace Vendor\Extname\Domain\Model;

interface AnswerOrStatusInterface
{
    // can be empty or include common methods of both types
}

class answer implements \Vendor\Extname\Domain\Model\AnswerOrStatusInterface
{
    ...
}

class status implements \Vendor\Extname\Domain\Model\AnswerOrStatusInterface
{
    ...
}

And another namespace for the controller:

namespace Vendor\Extname\Controller;

class AnswerController
{
    public function createAction(
        \Vendor\Extname\Domain\Model\Ticket $ticket, 
        \Vendor\Extname\Domain\Model\AnswerOrStatusInterface $answerOrStatus
    ) {
        $answer = null;
        $status = null;
        if (is_a($answerOrStatus, \Vendor\Extname\Domain\Model\answer)) {
            $answer = $answerOrStatus;
        }
        elseif (is_a($answerOrStatus, \Vendor\Extname\Domain\Model\status)) {
            $status = $answerOrStatus;
        }
        // further logic
        ...
    }
 }

In the PHP-Manual you can find more about

Starting with PHP 7.4 you still have more options: