[decided]
Hi!
I’m using yiiframework.com/extension/eadvancedarbehavior - extension for relations record into MySQL,
but i need to change value before insert into related table, and don’t understand how to make it =(
Thank’s.
Code Example:
public function actionCreate()
{
$model = new Book();
$topic = new Topic();
$author = new Author();
if (isset($_POST['Book'], $_POST['Author'], $_POST['Topic'])) {
$_POST['Book']['image'] = $_FILES['Book']['name']['image'];
$find_topic = stripcslashes(htmlspecialchars($_POST['Topic']['title']));
$topic_exist = Topic::model()->findBySql('SELECT * FROM topic WHERE title=:param', array(':param' => $find_topic));
$model->attributes = $_POST['Book'];
$topic->attributes = $_POST['Topic'];
$author->attributes = $_POST['Author'];
$valid = $model->validate();
$valid = $author->validate() && $valid;
$valid = $topic->validate() && $valid;
if ($valid) {
$random = rand(0, 99999);
$name = $_FILES['Book']['name']['image'];
$extension = pathinfo($name, PATHINFO_EXTENSION);
$newName = $random . '.' . $extension;
$model->image = CUploadedFile::getInstance($model, 'image');
$fullImgSource = Yii::getPathOfAlias('webroot') . '/images/' . $newName;
$model->image->saveAs($fullImgSource);
$model->image = $newName;
$author->save(false);
$model->save(false);
$topic->save(false);
$book = Book::model()->findByPk($model['id']);
$topics = Topic::model()->findAllByPk($topic['id']);
$authors = Author::model()->findAllByPk($author['id']);
// This helped
$find_topic = stripcslashes(htmlspecialchars($_POST['Topic']['title']));
$topic_exist = Topic::model()->findBySql('SELECT * FROM topic WHERE title=:param', array(':param' => $find_topic));
if ($topic_exist != '') {
$topics[0]['id'] = $topic_exist['id'];
$topics[0]['title'] = $topic_exist['title'];
}
$book->topics = $topics; //here makes relations
$book->authors = $authors; //here makes relations
$book->save();
// var_dump($book['onbeforesave'][0]);exit;
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
'topic' => $topic,
'author' => $author,
));
}