Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Issue with Insert Id

posted by ddsystems 8 years ago

When I try to store a record in a table with multiple indexes using active record it does not return a valid ID. I have a table called "Events"

CREATE TABLE IF NOT EXISTS events ( id int(11) NOT NULL AUTO_INCREMENT, startdate datetime NOT NULL, enddate datetime NOT NULL, text varchar(250) NOT NULL, link varchar(250) DEFAULT NULL, notify int(1) DEFAULT NULL, type varchar(100) DEFAULT NULL, companyid int(11) NOT NULL, propertyid int(11) NOT NULL, PRIMARY KEY (id,companyid) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=47 ;

Example Code:

$event = new event(); $event->setStartdate($startdate); $event->setEnddate($enddate); $event->setLink(fRequest::get("file")); $event->setNotify(fRequest::get("notify")); $event->setCompanyid($_loggedinUser->company->getId()); $event->setPropertyid($curProperty); $event->store();

After running this if I try and do $event->getId(); it returns null or blank.

This same process works if I remove the "companyid" from the primary key

ddsystems, it could be a bug in the way that Flourish is detecting what to update when a record is stored. I'd post a ticket. That being said, while it may make sense to index companyid, it doesn't seem to need to be part of your primary key given that the id using auto-increment will always be unique.

posted by mattsah 8 years ago

Yes, as it stands right now Flourish only supports auto-incrementing columns that are part of a single-column primary key. Like msahagian said, from a practical perspective, I'm not sure what the value would be in having a auto-incrementing column that is part of a multi-column primary key.

posted by wbond 8 years ago

I have it set up that way so that I can verify that the record belongs to the company using similar code to this

// Loading a record with a multiple column primary key

$event = new Event(array('id' => 2, 'companyid' => 3));

Is there a better way to go about doing this using activerecord? I'd rather not have to use a custom query if I can avoid it (would require a bit of rewriting at this point)

posted by ddsystems 8 years ago

That constructor call will also work if you set up a unique constraint over those two columns.

posted by wbond 8 years ago

Awesome, thanks for the advice.

posted by ddsystems 8 years ago