S
S
Sergey08082017-06-13 13:43:13
PHP
Sergey0808, 2017-06-13 13:43:13

PHP Phalcon how to add new robots to a specific user?

There are two models users and robots, established a relationship between them as one to many:

class Users extends Model
    {
        public $id;

        public $name;

        public $about;

        public $email;


        public function initialize()
        {
            $this->hasMany(
                "id",
                "Robots",
                "users_id"
            );
        }

class Robots extends Model

    {
        public $id;

        public $name;

        public $type;

        public $year;

        public $users_id;

        public function initialize()
        {
            $this->belongsTo(
                "users_id",
                "Users",
                "id"
            );
        }
    }

Add robot view:
<h2>
    Заполните эту форму, чтобы добавить своего робота!
</h2>

<?php echo $this->tag->form("robots/add"); ?>

<div>
    <div>
        <label for="name">Имя робота:</label>
    </div>

    <?php echo $this->tag->textField("name"); ?>

</div>

<div>
    <div>
        <label for="type">Тип робота:</label>
    </div>

    <?php echo $this->tag->textField("type"); ?>

</div>

<div>
    <div>
        <label for="year">Год создания робота:</label>
    </div>

    <?php echo $this->tag->textField("year"); ?>

</div>


<br>
<div>
    <?php echo $this->tag->submitButton("Добавить"); ?>
</div>
<?php echo $this->tag->endForm(); ?>

And controller:
public function addAction()
        {
            if ($this->request->isGet()) {
                $this->tag->prependTitle("Добавить робота :: ");
            }

           if ($this->request->isPost()) {

               $user = Users::findFirst($this->session->get("auth"));
               $userRobots = $user->getRobots();
               $userRobots->name = $this->request->getPost("name");
               $userRobots->type = $this->request->getPost("type");
               $userRobots->year = $this->request->getPost("year");

               if (!$userRobots->save()) {
                   echo "Произошли следующие проблемы: <br>";

                   $errors = $userRobots->getMessages();

                    foreach ($errors as $error) {
                        echo $error, "<br/>";
                    }

                }

                return $this->response->redirect([
                    'for' => 'user-show',
                    'name' => $user->name
                ]);
            }


        }

And I get this error:
Column 'Sergey' doesn't belong to any of the selected models (1), when preparing.
Where is my error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maximw, 2017-06-13
@maximw

The error is most likely that instead of the column name in the database, you are inserting data.
I didn't find a place in your code where this is done.

L
lxfr, 2017-06-13
@lxfr

I didn't understand something

$userRobots = $user->getRobots();
$userRobots->name = $this->request->getPost("name");

logically, you pull the robot-OB and then write $userRobots->name supposedly he is alone, don't you think this is strange?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question