Answer the question
In order to leave comments, you need to log in
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"
);
}
}
<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(); ?>
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
]);
}
}
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question