Hello, For the past three hours I've been trying to write this small piece of code and with about 30 stackoverflow and phalcon forum tabs open and out of sheer frustration I'm asking for your help.
I have two mysql tables:
CREATE TABLE `inventory` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`type` text NOT NULL,
`manufacturer` int(5) NOT NULL,
`model` text NOT NULL,
`entered_time` int(15) NOT NULL,
`entered_by` varchar(128) NOT NULL,
`picture` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE companies (
id int(9) NOT NULL AUTO_INCREMENT,
name varchar(128) NOT NULL,
address text NOT NULL,
phone varchar(32) NOT NULL,
entered_time int(15) NOT NULL,
picture varchar(128) NOT NULL,
email varchar(128) NOT NULL,
contact_person text NOT NULL,
PRIMARY KEY (id)
)
And two empty models for both of them.
Each item in the inventory
table has to be associated with a company in the companies
table based on inventory.manufacturer = companies.id
.
I know there's a way to establish relationships between those two tables but I can not figure out how to properly do it.
I've tried to use the query builder but I would only get results from the first table using this code:
use EQAS\Models\Inventory;
use EQAS\Models\Companies;
// [...]
$items = $this->modelsManager->createBuilder()
->from('\EQAS\Models\Inventory')
->join('\EQAS\Models\Companies', '[\EQAS\Models\Inventory].manufacturer = co.id', 'co', 'LEFT')
->getQuery()->execute();
I realized that the actual sql query looks like SELECT inventory.* FROM [...]
which means that the companies information is not being selected. While searching for solution I've seen a piece of code where someone uses the "columns" call to establish what is being selected. This however creates a mess out of the returned data and doesn't work.
The best solution for me would be to take advantage of the relationships and I would appreciate an example based on my code structure.
Best regards, Szymon.