Denoting multi-dimensional array data in relational database table? - database

Say I have an object-like data record like this:
$article = array(
'title' => '',
'tagline' => '',
'content' => '',
'stats' => array(
'words' => 0,
'paragraphs' => 0,
'tables' => 0
),
'references' => array(
'reference 1',
'reference 2',
'reference 3'
),
'attachments' => array(
'images' => array(
'image 1',
'image s'
),
'videos' => array(
'video 1',
'video 2'
)
)
);
My question is how can I store this array of data record in relational database? How should I design the table structure?
I know I can always set up flat fields such as stats_words, stats_paragraphs, and so forth but is there any more structural ways? Instead of storing a JSON or serialized string in a single field....
Thanks!

For example this way:
article
ID
title _
tagline _
content ___
stat_words
stat_paragraphs
stat_tables
article_reference
ID
article_id -> article
reference _
article_attachment
ID
article_id -> article
att_type // image or video
path _
title _
(_ means varchar/text fields, other fields are numbers)
Or as MySQL DDL:
CREATE TABLE IF NOT EXISTS article (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
tagline VARCHAR(255) NOT NULL,
content MEDIUMTEXT NOT NULL,
stat_words INT NOT NULL,
stat_paragraphs INT NOT NULL,
stat_tables INT NOT NULL,
PRIMARY KEY ( id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS article_reference (
id INT NOT NULL AUTO_INCREMENT,
article_id INT NOT NULL,
reference VARCHAR(255) NOT NULL,
PRIMARY KEY ( id ),
FOREIGN KEY ( article_id ) REFERENCES article( id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS article_attachment (
id INT NOT NULL AUTO_INCREMENT,
article_id INT NOT NULL,
att_type INT NOT NULL,
path VARCHAR(255) NOT NULL,
title VARCHAR(255) NOT NULL,
PRIMARY KEY ( id ),
FOREIGN KEY ( article_id ) REFERENCES article( id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

Related

Issue with setDataSource in CakePHP 2.4.8

I wrote a behavior, Cascadable, that merges records across 2 databases. It works sort of like CSS, where the more local style overrides the global. The behavior uses $this->setDataSource() to switch between the databases. This was working up until the 2.4.8 release when this commit was made to the Model.php file.
Here's sort of an extraction from the behavior:
$Model->setDataSource( 'alt' );
$altData = $Model->find($findType, array(
'contain' => false,
'conditions' => array(
$Model->alias.'.id' => $ids
),
'callbacks' => 'before'
));
If the callbacks option is set to false, I get Missing Database Table errors about tables that are only present in the default (parent) datasource.
I'm not sure what I'm doing wrong here, but after 2.4.8 upgrade it always uses the wrong (parent) database instead of the child. I can revert Model.php back to the 2.4.7 version to fix the issue. I realize this is difficult to replicate because it only breaks in my very specific database structure.
MySQL Table Schema example:
Parent Database Table
CREATE TABLE `products` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`price` float(8,2) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `active` (`active`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 PACK_KEYS=0;
INSERT INTO `parent_db`.`products` (`id`, `name`, `price`, `active`) VALUES (NULL, 'Big Box of Goodies', '99.99', '0');
Child Database Table
CREATE TABLE `products` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`price` float(8,2) DEFAULT NULL,
`active` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `active` (`active`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 PACK_KEYS=0;
INSERT INTO `child_db`.`products` (`id`, `name`, `price`, `active`) VALUES (NULL, 'Small Box of Surprises', NULL, '1');
Query Results
From Parent DB
array(
(int) 0 => array(
'Product' => array(
'id' => '1149',
'name' => 'Big Box of Goodies',
'price' => '99.99',
'active' => false
)
)
)
From Child DB with working Cascadable behavior (child record is merged into parent record, ignoring null values)
array(
(int) 0 => array(
'Product' => array(
'id' => '1149',
'name' => 'Small Box of Surprises',
'price' => '99.99',
'active' => true
)
)
)
Workaround:
Setting the schemaName manually after using setDataSource fixes the issue for me.
$Model->setDataSource( 'alt' );
App::uses('ConnectionManager', 'Model');
$dataSources = ConnectionManager::enumConnectionObjects();
$Model->schemaName = $dataSources['alt']['database'];

Polymorphic associations in CakePHP2

I have 3 models, Page , Course and Content
Page and Course contain meta data and Content contains HTML content.
Page and Course both hasMany Content
Content belongsTo Page and Course
To avoid having page_id and course_id fields in Content (because I want this to scale to more than just 2 models) I am looking at using Polymorphic Associations. I started by using the Polymorphic Behavior in the Bakery but it is generating waaay too many SQL queries for my liking and it's also throwing an "Illegal Offset" error which I don't know how to fix (it was written in 2008 and nobody seems to have referred to it recently so perhaps the error is due to it not having been designed for Cake 2?)
Anyway, I've found that I can almost do everything I need by hardcoding the associations in the models as such:
Page Model
CREATE TABLE `pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
<?php
class Page extends AppModel {
var $name = 'Page';
var $hasMany = array(
'Content' => array(
'className' => 'Content',
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Page'),
)
);
}
?>
Course Model
CREATE TABLE `courses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
<?php
class Course extends AppModel {
var $name = 'Course';
var $hasMany = array(
'Content' => array(
'className' => 'Content',
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Course'),
)
);
}
?>
Content model
CREATE TABLE IF NOT EXISTS `contents` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`class` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`foreign_id` int(11) unsigned NOT NULL,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
<?php
class Content extends AppModel {
var $name = 'Content';
var $belongsTo = array(
'Page' => array(
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Page')
),
'Course' => array(
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Course')
)
);
}
?>
The good thing is that $this->Content->find('first') only generates a single SQL query instead of 3 (as was the case with the Polymorphic Behavior) but the problem is that the dataset returned includes both of the belongsTo models, whereas it should only really return the one that exists. Here's how the returned data looks:
array(
'Content' => array(
'id' => '1',
'class' => 'Course',
'foreign_id' => '1',
'title' => 'something about this course',
'content' => 'The content here',
'created' => null,
'modified' => null
),
'Page' => array(
'id' => null,
'title' => null,
'slug' => null,
'created' => null,
'updated' => null
),
'Course' => array(
'id' => '1',
'title' => 'Course name',
'slug' => 'name-of-the-course',
'created' => '2012-10-11 00:00:00',
'updated' => '2012-10-11 00:00:00'
)
)
I only want it to return one of either Page or Course depending on which one is specified in Content.class
UPDATE: Combining the Page and Course models would seem like the obvious solution to this problem but the schemas I have shown above are just shown for the purpose of this question. The actual schemas are actually very different in terms of their fields and the each have a different number of associations with other models too.
UPDATE 2
Here is the query that results from running $this->Content->find('first'); :
SELECT `Content`.`id`, `Content`.`class`, `Content`.`foreign_id`, `Content`.`title`,
`Content`.`slug`, `Content`.`content`, `Content`.`created`, `Content`.`modified`,
`Page`.`id`, `Page`.`title`, `Page`.`slug`, `Page`.`created`, `Page`.`updated`,
`Course`.`id`, `Course`.`title`, `Course`.`slug`, `Course`.`created`,
`Course`.`updated` FROM `cakedb`.`contents` AS `Content`
LEFT JOIN `cakedb`.`pages` AS `Page` ON
(`Content`.`foreign_id` = `Page`.`id` AND `Content`.`class` = 'Page')
LEFT JOIN `cakedb`.`courses` AS `Course` ON (`Content`.`foreign_id` = `Course`.`id`
AND `Content`.`class` = 'Course') WHERE 1 = 1 LIMIT 1
Your query is okay. Just filter empty values after find:
public function afterFind($results, $primary = false) {
return Set::filter($results, true);
}
Take a look at this question.
You can also try to provide conditions to you find query that Page.id in not null or Course.id is not null.

CakePHP: HABTM and Recursive attribute to get all related data

I'm in trouble with HABTM relationship in my model. I searched for examples and references on this site and many others for a couple of weeks without success. The clasical Posts HABTM Tags official documentation works well but extrapolated to my model fails. All information found does not contain examples or references for composite primary keys (I suspect the relationship will not work because of the composite primary keys and I can`t change this schema)
My goal is to get the Relationship:
Customer HABTM Products.
So, with
$customers = $this->Customer->find('all');
Get the array of all products related to a customer in the Customer object .
Currently I canĀ“t get this to work: $customer does not have the spected Products array.
I appreciate any help or reference to help me fix this.
This is my db:
CREATE TABLE `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lang` varchar(2) NOT NULL,
`name` varchar(145) NOT NULL,
`logo` varchar(145) NOT NULL,
`description` text,
PRIMARY KEY (`id`,`lang`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lang` varchar(2) NOT NULL,
`category_id` int(11) NOT NULL,
`service_id` int(11) NOT NULL,
`description` text,
`picture` varchar(145) NOT NULL,
PRIMARY KEY (`id`,`category_id`,`service_id`,`lang`),
KEY `fk_products_services1` (`service_id`,`lang`),
KEY `fk_products_categories1` (`category_id`,`lang`),
CONSTRAINT `fk_products_categories1` FOREIGN KEY (`category_id`, `lang`) REFERENCES `categories` (`id`, `lang`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_products_services1` FOREIGN KEY (`service_id`, `lang`) REFERENCES `services` (`id`, `lang`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8
CREATE TABLE `customer_products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`customer_id`,`product_id`),
KEY `fk_customer_products_products1` (`product_id`),
KEY `fk_customer_products_customers` (`customer_id`),
CONSTRAINT `fk_customer_products_customers` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_products_products1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
This is my Customer model:
<?php
App::uses('AppModel', 'Model');
/**
* Customer Model
*
* #property CustomerProduct $CustomerProduct
* #property Product $AllProducts
*/
class Customer extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'name';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasAndBelongsToMany associations
*
* #var array
*/
public $hasAndBelongsToMany = array(
'Products' => array(
'className' => 'Product',
'joinTable' => 'customer_products',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'product_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
This is my index Customer controller method:
public function index() {
$this->layout = 'header_chico';
$this->Customer->recursive = 0;
$customers = $this->Customer->find('all');
$this->set('customers', $customers);
}
Your HABTM table should be customers_products (not customer_products). That will probably fix it. Remember the format is both plural in alphabetical order. So if it's customers HABTM products it is customers_products. If it were users HABTM products it would be products_users.
UPDATE
If you want the HABTM records to appear in the index, you also need to remove:
$this->Customer->recursive = 0;
This will prevent related records from appearing.

cakephp - Followers relationship

Im trying to create a User Connections relationship:
Following (people im following).
Followers (people who follows me).
My question:
It is a self-relationship? How do I define this?
Thanks in advance.
Untested, but somethinglike this should work:
class User extends AppModel {
var $hasAndBelongsToMany = array(
'Friend' => array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
),
'Follower' => array(
'className' => 'User',
'joinTable' => 'followers',
'foreignKey' => 'user_id',
'associationForeignKey' => 'follower_id',
)
);
}
Where you've got your standard users table, plus a friends table with user_id and friend_id fields, and a followers table with user_id and follower_id fields.
What you are looking for is a HABTM relationship. Although you will be referring twice to the same model (User) instead to two separate ones form the join-table.
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NULL ,
`password` VARCHAR(100) NULL ,
`email` VARCHAR(100) NULL ,
`created` DATETIME NULL ,
`modified` DATETIME NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `followers_users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `followers_users` (
`user_id` INT NOT NULL ,
`follower_id` INT NOT NULL ,
PRIMARY KEY (`user_id`, `follower_id`) ,
INDEX `fk_followers_users_users1` (`follower_id` ASC) ,
CONSTRAINT `fk_followers_users_users`
FOREIGN KEY (`user_id` )
REFERENCES `users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_followers_users_users1`
FOREIGN KEY (`follower_id` )
REFERENCES `users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
You can create a FollowersController that extends UsersController and then further define the relationship in the follower model. So you would actually have a model to refer back to.
I hope this helps.

Help with CakePHP Model Relationships

Three simple tables...
Feedname (eg. News or
Events) which are the names of RSS
feeds.
Posts that belong to a
Feedname
User, that owns all
the posts
I want to use the Form helper to automatically give me a select box so that when I add a post I can select which Feedname to assign it to.
It seems like posts belong to both Feedname and User but I can't get the correct combination of belongsTo and hasMany in my model/ .php files. The select box for feedname is shown, but there is nothing in it. Can anyone point me in the right direction?
The tables look like this at the moment:
CREATE TABLE `feednames` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`body` text COLLATE utf8_unicode_ci,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`user_id` int(10) unsigned NOT NULL DEFAULT '1',
`feedname_id` int(10) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `foreign_key` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET latin1 NOT NULL,
`password` char(40) CHARACTER SET latin1 NOT NULL,
`group_id` int(11) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
edit - adding the model .php files ...
class Feedname extends AppModel {
var $name = 'Feedname';
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'feedname_id',
'dependent' => false
)
);
}
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Feedname' => array(
'foreignKey' => 'feedname_id'
)
);
}
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false
)
);
}
edit - adding SQL dump ** ...
/posts/index.ctp:
SELECT COUNT(*) AS count FROM posts AS Post LEFT JOIN users AS User ON (Post.user_id = User.id) LEFT JOIN feednames AS Feedname ON (Post.feedname_id = Feedname.id) WHERE 1 = 1
SELECT Post.id, Post.title, Post.body, Post.created, Post.modified, Post.user_id, Post.feedname_id, User.id, User.username, User.password, User.group_id, User.created, User.modified, Feedname.id, Feedname.name, Feedname.created, Feedname.modified FROM posts AS Post LEFT JOIN users AS User ON (Post.user_id = User.id) LEFT JOIN feednames AS Feedname ON (Post.feedname_id = Feedname.id) WHERE 1 = 1 ORDER BY Post.created DESC LIMIT 10
Please note: /posts/add.ctp does not produce any SQL dump, so it's not getting the select box options from the database, this is what I'm trying to fix with proper model relationships.
Do you have something like this in your controller methods (e.g. the admin_add / admin_edit functions)?
$feednames = $this->Feedname->find('list');
$this->set('feednames', $feednames);
Cake should then automatically populate the select list with these values. Or you can manually set the values with:
$form->input('feedname_id', array('options' => $feednames));
So far I agree :
posts > belongs to > users
posts > belongs to > feednames
feednames > has many > posts
users > has many > posts
Just checking, but, did you actually insert data into your tables? Else it would be only logical that your select field is empty. Also, what does the Sql dump say in debug mode?

Resources