3 tables, 3 models: Sections, Lectures, SectionsHasLectures Relation is saved, Section is saved, Lectures is updated and set all values to NULL. What am I doing wrong? Thank you.
<?php
class Sections extends Model {
public function beforeValidationOnCreate()
{
// Timestamp the confirmaton
$this->publishdon = time();
}
public function initialize()
{
$this->hasManyToMany(
"id",
"Models\SectionsHasLectures",
"sectionId", "lectureId",
"Models\Lectures",
"id",
['alias' => 'lectures']
);
}
<?php
class Lectures extends Model {
public function beforeValidationOnCreate()
{
// Timestamp the confirmaton
$this->publishdon = time();
}
public function initialize()
{
$this->useDynamicUpdate(true);
$this->hasManyToMany(
"id",
"Models\SectionsHasLectures",
"sectionId",
"lectureId",
"Models\Sections",
"id",
['alias' => 'sections']
);
}
<?php
class SectionsHasLectures extends Model {
public $id;
public $sectionId;
public $lectureId;
public $created;
public $modified;
public function beforeValidationOnCreate()
{
$this->created = time();
}
public function initialize()
{
$this->belongsTo('sectionId', 'Models\Sections', 'id', array(
'alias' => 'section'
));
$this->belongsTo('lectureId', 'Models\Lectures', 'id', array(
'alias' => 'lecture'
));
}
Try to keep with the existing Lectures (in the form select the existing lectures):
<?php
$section = new Sections();
$lectures = $this->request->getPost('lectures');
$_lectures = array();
Lectures::setup(array('notNullValidations' => false));
foreach ($lectures as $lectureId) {
$lecture = new Lectures();
$lecture->id = $lectureId;
$_lectures[] = $lecture;
}
$section->assign(array(
'title' => $this->request->getPost('title', 'striptags'),
'speakerId' => $this->request->getPost('speakerId', 'int'),
'desc' => $this->request->getPost('desc'),
'published' => $published,
'pubDate' => strtotime($this->request->getPost('pubDate')),
'unpubDate' => strtotime($this->request->getPost('unpubDate')),
));
if (!$section->save()) {
foreach ($section->getMessages() as $message) {
$this->flashSession->error($message);
}
} else {
$section->lectures = $_lectures;
$section->update();
$this->flashSession->success("Секция успешно добавлена");
}