Hey guys
I have a big issue about relational database in Cakephp. I built a databases that involve, among others a many to many relation between two tables. Therefor I made three tables “branches”, “projects” and the link between them to materialize the relation, “branches_projects”, as follows
CREATE TABLE branches (
branch_id INTEGER NOT NULL AUTO_INCREMENT,
namebranch VARCHAR(50),
PRIMARY KEY(branch_id)
) ENGINE = InnoDB;
CREATE TABLE projects (
project_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
beginning DATE,
end DATE,
task_id INTEGER,
PRIMARY KEY(project_id),
CONSTRAINT FOREIGN KEY (task_id) REFERENCES tasks (task_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
CREATE TABLE branches_projects (
branch_id INTEGER,
project_id INTEGER,
PRIMARY KEY(branch_id,project_id),
CONSTRAINT FOREIGN KEY (branch_id) REFERENCES branches (branch_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (project_id) REFERENCES projects (project_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
Additionally, I made other tables, one of them called “tasks” which is linked with a one to many relation with “projects” (“tasks” is also linked with other tables, but that is not important now)
CREATE TABLE tasks (
task_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(250),
status VARCHAR(50),
visit_id INTEGER,
blueprint_id INTEGER,
rdi_id INTEGER,
payment_status_id INTEGER,
hes_id INTEGER,
purchase_order_id INTEGER,
PRIMARY KEY(task_id),
CONSTRAINT FOREIGN KEY (visit_id) REFERENCES visits (visit_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (blueprint_id) REFERENCES blueprints (blueprint_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (rdi_id) REFERENCES rdis (rdi_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (payment_status_id) REFERENCES payment_status (payment_status_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (hes_id) REFERENCES hes (hes_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (purchase_order_id) REFERENCES purchase_orders (purchase_order_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
I baked them, receiving a message sort of “cake can´t build all relations this time”. The link between “projects” and “tasks” works perfectly, but the link with “branches does not. It generates the three tables, but I intended to have a table where I could select the project and the branch where it belongs, along with the tasks associated. Here is the ProjectController table (the view function part)
public function view($id = null)
{
$project = $this->Projects->get($id, ['contain' => ['Tasks']
]);
//I added this line
$project = $this->Projects->get($id, ['contain' => ['Branches']]);
//end
$this->set('project', $project);
}
please help…