Open
Description
I have the following (MySQL) database schema:
CREATE TABLE `jobs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`export_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `exports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` VARCHAR(1000) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB DEFAULT CHARSET=utf8;
With the following PHP ActiveRecord models:
class Job extends ActiveRecord\Model {
// A Job has 0 or 1 Export
// (but we use belongs to as the foreign key is on the Job table)
static $belongs_to = [['export']];
// A Job has a name
static $attr_accessible = ['name'];
}
class Export extends ActiveRecord\Model {
// An Export has a URL
static $attr_accessible = ['url'];
}
When I run the following code, I expected export_id
to be the same as Export::last()->id
but it is null
:
$job = new Job(["name" => "testJob"]);
$job->create_export([
"url" => "http://www.example.org"
]);
$job->save();
$job->reload();
var_dump($job->export_id); // prints NULL
Interestingly, $job->name
has the correct value (testJob
). The MySQL database has the following data:
mysql> select * from jobs;
+----+---------+-----------+
| id | name | export_id |
+----+---------+-----------+
| 1 | testJob | NULL |
+----+---------+-----------+
mysql> select * from exports;
+----+------------------------+
| id | url |
+----+------------------------+
| 1 | http://www.example.org |
+----+------------------------+
I've tried the code on both v1.1.2 and the master branch, and am getting the same results on both versions.
Any hints as to what I might be doing wrong? Could this be a bug?
Activity
visavi commentedon Jun 29, 2016
structure should be like this
If there is no condition of links quite simply write
static $belongs_to = ['export'];
but in your case should be a link $has_many or $has_one
then when such entries
whereas when such record create_export automatic fill field job_id
and yes I also have a question to the developers about filling if a many-to-many through the staging table in version 1.1
many have not tried it was impossible to fill all 3 tables
We have to first save the news
and then do something like this
koenpunt commentedon Jun 30, 2016
This is probably because you create the export before the job has been saved. Try calling
$job->save();
beforecreate_export
louismrose commentedon Jun 30, 2016
Thanks for your quick replies, @visavi and @koenpunt
I've tried changing the code to this:
And to this:
But in both cases
$job->export_id
is stillnull
.I'd like to keep the foreign key on the Jobs table if possible. However, I tried the suggestion of @visavi and updated the MySQL schema such that the foreign key was on the Export table and was named
job_id
. However, I got the following exception:Which seems to indicate that belongs_to is correctly looking for the foreign key attribute on the Jobs?
koenpunt commentedon Jun 30, 2016
To add to @visavi's answer, when going with the schema he suggests, you'll have to update the
belongs_to
association to ahas_one
louismrose commentedon Jun 30, 2016
I'd really like to keep the relationship as a "belongs to" (i.e., the foreign key on the Job table) if at all possible.
Interestingly, when I run this code:
I get the following exception:
But when I run:
Then
null
is printed out!So it seems there is an inconsistency: I can read from the
export
property but not write to it. Perhaps this is causing Relationship.php to not have the correct effect when it associates the Export with the Job?Have I configured something incorrectly when setting up my belongs to association or DB schema?
louismrose commentedon Jul 5, 2016
Just so I can be a little more sure that it's perhaps not my setup... I've been able to recreate this issue with the following new PHP AR unit test, which I've added to Relationship.php:
The test fails with the following output:
Which seems to imply that the event's foreign key column (venue_id) has not been set to the ID of the new venue (10). Looking at the fixtures, it seems that this event (ID 5) has venue_id 6 by default.
Any ideas what the issue might be @koenpunt, @visavi ?
Corrects record appending for belongs_to jpfuentes2#546
Corrects record appending for belongs_to jpfuentes2#546
louismrose commentedon Jul 5, 2016
Ok, I think I might have a solution for this... I've cooked up PR #548.