josegonzalez / cakephp-version

CakePHP3: plugin that facilitates versioned database entities
MIT License
51 stars 22 forks source link

Versions get saved but can't retrieve them in my views #4

Closed chonz0 closed 7 years ago

chonz0 commented 9 years ago

Hi,

First of all, thanks a lot for your awesome work :-) I'm trying to implement your awesome plugin, but I'm having some troubles with it, and I think it may be a bug...

The records (versions) do get saved correctly to the database, but whenever I try to show it in my view, I get nothing... :-(

Am I missing something?

Thanks in advance! Gonzalo

src/Model/Entity/Page.php

<?php
//namespace App\Model\Entity;
namespace SiteManager\Model\Entity;

use Cake\ORM\Entity;
use \Josegonzalez\Version\Model\Behavior\Version\VersionTrait;

/**
 * Page Entity.
 */
class Page extends Entity
{
    use VersionTrait;

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * @var array
     */
    protected $_accessible = [
        'title' => true,
        'content' => true,
        'album_id' => true
    ];
}

src/Model/Table/PagesTable.php

class PagesTable extends Table
{
    public function initialize(array $config)
    {
        $this->table('pages');
        $this->displayField('title');
        $this->primaryKey('id');
        $this->addBehavior('Timestamp');
        $this->belongsTo('Albums', [
            'foreignKey' => 'album_id',
            'className' => 'SiteManager.Albums'
        ]);

        $this->addBehavior('Josegonzalez/Version.Version');
}

src/Controller/PagesController.php

    public function view($id = null)
    {
        $page = $this->Pages->get($id, [
            'contain' => []
        ]);

        $version_3 = $page->version(3);
        debug($version_3); // outputs: null

        $versions = $page->versions();
        debug($versions); // outputs: []
        $this->set('versions', $versions);

        $this->set('page', $page);
        $this->set('_serialize', ['page']);

        debug($page); // outputs: see next block 
    }
object(SiteManager\Model\Entity\Page) {

    'id' => (int) 21,
    'title' => 'Contact',
    'content' => '<p>Maecenas ullamcorper, dui et placerat feugiat, eros pede varius nisi, condimentum viverra felis nunc et lorem. Vivamus euismod mauris. Praesent nonummy mi in odio. Praesent venenatis metus at tortor pulvinar varius. Sed a libero.</p>',
    'image' => null,
    'slug' => 'contact',
    'album_id' => null,
    'version_id' => (int) 4,
    'created' => object(Cake\I18n\Time) {

        'time' => '2015-07-24T01:42:11+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    '_versions' => [],
    '[new]' => false,
    '[accessible]' => [
        'title' => true,
        'content' => true,
        'album_id' => true
    ],
    '[dirty]' => [
        '_versions' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'SiteManager.Pages'

}
josegonzalez commented 9 years ago

Do you get any database queries from the call to versions?

chonz0 commented 9 years ago

Thanks for replying :-)

Yes, according to DebugKit, when I call

$versions = $page->versions(); 

I get the following queries in SQL log:

SHOW FULL COLUMNS FROM `version`    7   8
SHOW INDEXES FROM `version` 1   1
SELECT * FROM information_schema.key_column_usage AS kcu INNER JOIN information_schema.referential_constraints AS rc ON ( kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME AND kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA ) WHERE kcu.TABLE_SCHEMA = 'cakephp3cms' AND kcu.TABLE_NAME = 'version' AND rc.TABLE_NAME = 'version'   0   16
SHOW TABLE STATUS WHERE Name = 'version'    1   1
SELECT version.id AS `version__id`, version.version_id AS `version__version_id`, version.model AS `version__model`, version.foreign_key AS `version__foreign_key`, version.field AS `version__field`, version.content AS `version__content`, version.created AS `version__created` FROM version version INNER JOIN (SELECT (Pages.id) FROM pages Pages GROUP BY Pages.id ) Pages ON version.foreign_key = (Pages.id) WHERE (version.model = 'Pages' AND version.foreign_key in (21) AND field in ('id','title','content','image','slug','album_id','version_id','created')) 24  1

The last query returns 24 rows (these are all the records I currently have in my ´version´table)

BTW: I'm from Argentina, so my main language is spanish, but I wanted to write in english just in case someone else could help too

josegonzalez commented 9 years ago

Can you create a sample app for me (including data in your database) so I can test this out?

chonz0 commented 9 years ago

Ok, so I created a brand new clean Cake3 app, set up everything to work with my custom plugin, then installed your awesome CakePHP Version plugin and... voila! I have no idea what was wrong with my setup... I'll do a little more research on it, but so far I have no clue :S If it happens to be something with the plugin I'll let you know, but it looks like it was something else. Thank you -again- very much for your time and your great work! Cheers! Gonzalo

chonz0 commented 9 years ago

I looks like the version plugin is having trouble with the my 'pages' model. I tried a new one, 'sections', and everything keeps working great (as in my 'posts' model).

chonz0 commented 9 years ago

Sorry... Reopening this...

I made a fresh install of CakePHP, baked MVC for a couple tables (posts & sections), to be sure it wasn't an issue with the 'pages' naming... But the version plugin keeps acting weird... I've observed that: 1) In 'version' table, the 'version_id' field keeps incrementing as an auto-increment field, it does not keep track of version per foreign_key 2) Besides that, all data seems to be saved OK, the issue comes when I have to read the versions using $model->versions(); The first entry is read correctly, but the second does not.

I've uploaded the app to a GitHub repo: https://github.com/chonz0/cakephp-version-app Inside the 'mysql_dump' folder, there is an exact copy of my database for you to import it and see if you face the same issue.

Thanks in advance, Gonzalo

chonz0 commented 9 years ago

Ok, I think I got something...

In VersionTrait.php, inside the versions() method, I replaced:

        $conditions = ['primaryKey' => $this->id];

with:

        $conditions = [ 'conditions' => ['id' => $this->id] ];

and now I do get all versions of each entry as desired (maybe, instead of 'id' => $this->id we should use something generic, like $model.'.'.$primaryKey, but I don't know how to do that.

dereuromark commented 7 years ago

This looks like resolved in current master?