We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Many-to-many bug

This does not save the tags defined by the many-to-many relationship:

$article = new Article();

$title = $this->request->getPost('title');
$user_id = $this->session->user_id;
$timestamp = time();
$content = $this->request->getPost('content');
$sticky = $this->request->getPost('sticky') ?: 0;
$tags = array();

foreach (explode(',', $this->request->getPost('tag_names')) as $tag_name) {
    $tag = Tag::findFirstByName($tag_name);

    if (!$tag) {
        $tag = new Tag();
        $tag->name = $tag_name;
    }

    $tags[] = $tag;
}

$success = $article->save(array(
    'title' => $title,
    'user_id' => $user_id,
    'timestamp' => $timestamp,
    'content' => $content,
    'sticky' => $sticky,
    'tags' => $tags
));

However the following works:

$article = new Article();
$article->title = $this->request->getPost('title');
$article->user_id = $this->session->user_id;
$article->timestamp = time();
$article->content = $this->request->getPost('content');
$article->sticky = $this->request->getPost('sticky') ?: 0;
$tags = array();

foreach (explode(',', $this->request->getPost('tag_names')) as $tag_name) {
    $tag = Tag::findFirstByName($tag_name);

    if (!$tag) {
        $tag = new Tag();
        $tag->name = $tag_name;
    }

    $tags[] = $tag;
}

$article->tags = $tags;

$success = $article->save();

I think this is clearly a bug but I will post the models and schema anyways in case I am doing something wrong:

<?php

use Phalcon\Mvc\Model;

class Article extends Model
{

    public $id;

    public $title;

    public $user_id;

    public $timestamp;

    public $content;

    public $sticky;

    public function initialize()
    {
        $this->belongsTo('user_id', 'User', 'id', array(
            'foreignKey' => true
        ));

        $this->hasManyToMany('id', 'ArticleTag', 'article_id', 'tag_id', 'Tag', 'id', array(
            'alias' => 'tags',
            'foreignKey' => true
        ));
    }

}

<?php

use Phalcon\Mvc\Model,
    Phalcon\Mvc\Model\Validator\Uniqueness;

class Tag extends Model
{

    public $id;

    public $name;

    public function initialize()
    {
        $this->hasManyToMany('id', 'ArticleTag', 'tag_id', 'article_id', 'Article', 'id', array(
            'alias' => 'articles',
            'foreignKey' => true
        ));
    }

    public function validation()
    {
        $this->validate(new Uniqueness(
            array(
                'field' => 'name',
                'message' => 'The name provided already exists.'
            )
        ));

        return !$this->validationHasFailed();
    }

}

<?php

use Phalcon\Mvc\Model;

class ArticleTag extends Model
{

    public $id;

    public $article_id;

    public $tag_id;

    public function initialize()
    {
        $this->belongsTo('article_id', 'Article', 'id', array(
            'foreignKey' => 'true'
        ));

        $this->belongsTo('tag_id', 'Tag', 'id', array(
            'foreignKey' => 'true'
        ));
    }

 }

CREATE TABLE IF NOT EXISTS `article` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `title` varchar(150) NOT NULL,
  `user_id` int(10) NOT NULL,
  `timestamp` int(10) NOT NULL,
  `content` varchar(20000) NOT NULL,
  `sticky` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `article_tag` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `article_id` int(10) NOT NULL,
  `tag_id` int(10) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `article_id` (`article_id`,`tag_id`),
  KEY `tag_id` (`tag_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `tag` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

ALTER TABLE `article_tag`
  ADD CONSTRAINT `article_tag_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`),
  ADD CONSTRAINT `article_tag_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`);

Any validation error?



5.6k

Any validation error?

No, it saves without any error. Just the tags aren't saved.