Using RedbeanPHP without "id" column

2,592 views
Skip to first unread message

Jay

unread,
Feb 11, 2011, 12:30:44 PM2/11/11
to redbeanphp
Hi everyone,

Here's my code. I would like to use the ID column as the main column
and not as an auto-incrementing integer. When running the following,
the table gets created, but nothing inserted as "UPDATE `sessions` SET
`expire` = ? , `session_data` = ? WHERE `id` = 9Array ( [0] =>
1297446793 [1] => testvariabel|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;} ) " is
run due to the "id" column being defined.

What should I do to accomplish what I want?

function write($id, $sessiondata) {

$sessionObject = $this->db->dispense("sessions");

$sessionObject->id = $id;
$sessionObject->expire = (time() + $this->ttl);
$sessionObject->session_data = $sessiondata;

$id = $this->db->store( $sessionObject );

var_dump($id);


return TRUE;


}

gabor

unread,
Feb 11, 2011, 4:22:06 PM2/11/11
to redbeanphp

Have you tried to use a bean formatter?

http://www.redbeanphp.com/#/Prefixes

I think that solves the problem, let me know ;)

Jay

unread,
Feb 12, 2011, 5:00:36 AM2/12/11
to redbeanphp
Hi Gabor,

Maybe I wasn't clear in my first question.

I was however aware of the bean formatters before posting my question,
but didn't thought that it would help me as prefixes / bean formatters
seems to affect all tables and not help me get rid of the default,
auto-incrementing "id"-column and be able to use my own id column as
key->value -storage.

I need to use my own column as Primary to store my sessions in my
table. Can you see what I mean?


Thanks!

gabor de mooij

unread,
Feb 12, 2011, 7:15:17 AM2/12/11
to redbe...@googlegroups.com
Formatters dont affect all tables, the fomatter gets called like this:

$formatter->formatBeanID( "session" )

You can then say in your formatter:

function formatBeanID( $table ) {
if ($table=="session") return $myDifferentID;
}

RedBean always needs a primary key, you can rename the key but it is
impossible (not entirely of course) by design to have tables with no
primary key at all. I would recommend to simply use a bean formatter
or rename the field in your DB. If this is the only table in your DB
with this problem you may also opt to write custom queries for that
one.

There is of course the possibility to make a plugin to overrule the
behavior of RB but I think it's not worth the trouble.
Does this help?

Cheers,
Gabor

> --
> You received this message because you are subscribed to the Google Groups "redbeanphp" group.
> To post to this group, send email to redbe...@googlegroups.com.
> To unsubscribe from this group, send email to redbeanorm+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/redbeanorm?hl=en.
>
>

Jay

unread,
Feb 12, 2011, 7:22:49 AM2/12/11
to redbeanphp
Hi again,

Great to know that formatters doesnt affect all tables when called as
shown by you.

Sorry for all the confusion here and many thanks for your support even
though I am writing like a monkey:
I am going to have a primary id, but that's will be the PHP native
session id and not an auto incrementing integer, like Redbean seems to
default itself to. The session ID is the one that PHP figures out by
it self.

I really love the fact that redbeanphp figures out when to update/
insert by itself and I would love to have this feature, but without
having an auto-incrementing integer in my table, causing havoc on the
logic. Does this make things clearer?

Storing a bean without a column named explicit "id" will obviously
create a ID column with a


On 12 Feb, 13:15, gabor de mooij <gabordemo...@gmail.com> wrote:
> Formatters dont affect all tables, the fomatter gets called like this:
>
> $formatter->formatBeanID( "session" )
>
> You can then say in your formatter:
>
> function formatBeanID( $table ) {
>   if ($table=="session") return $myDifferentID;
>
> }
>
> RedBean always needs a primary key, you can rename the key but it is
> impossible (not entirely of course) by design to have tables with no
> primary key at all. I would recommend to simply use a bean formatter
> or rename the field in your DB. If this is the only table in your DB
> with this problem  you may also opt to write custom queries for that
> one.
>
> There is of course the possibility to make a plugin to overrule the
> behavior of RB but I think it's not worth the trouble.
> Does this help?
>
> Cheers,
> Gabor
>

Jay

unread,
Feb 12, 2011, 8:03:28 AM2/12/11
to redbeanphp
Here's some output which may help to describe what I mean:

RedBean Code:
$sessionObject = $this->db->dispense("sessions");
$sessionObject->id = $id;
$sessionObject->expire = (time() + $this->ttl);
$sessionObject->session_data = $sessiondata;
$id = $this->db->store( $sessionObject );
var_dump($id);

mySQL query from debug:
UPDATE `sessions` SET `expire` = ? , `session_data` = ? WHERE `id` =
9Array ( [0] => 1297517061 [1] => testvariabel|a:3:{i:0;i:1;i:1;i:2;i:
2;i:3;} )


What I want:
INSERT INTO `sessions` ( `id`, `expire`,`session_data` ) VALUES
( 9vqfjq8gn5v13k6fjddav5ql03 , 1297517121 , testvariabel|a:3:{i:0;i:
1;i:1;i:2;i:2;i:3;} )


Thanks,
Message has been deleted

Jay

unread,
Feb 13, 2011, 6:19:19 AM2/13/11
to redbeanphp
Hi again,

I've lifted out the Redbean to a separate PHP file to make the output
more understandable:

R::setup( "mysql:host=192.168.1.1;dbname=myTable",'root','root' );
R::debug(true);

class CustomBeanFormat implements RedBean_IBeanFormatter{
public function formatBeanTable($table) {
return $table;
}
function formatBeanID( $table ) {
if ($table=='sessions') return 'session_id';
}
}

R::$writer->setBeanFormatter(new CustomBeanFormat());

$sessionObject = R::dispense("sessions");
$sessionObject->session_id = 'gos81vu0ss9tl8tui959knrdo5';
$sessionObject->expire = (time() + 3600);
$sessionObject->session_data = serialize( array('bla','bla','bla') );

$id = R::store( $sessionObject );

Debug outputs:
UPDATE `sessions` SET `expire` = ? , `session_data` = ? WHERE
session_id = 0Array ( [0] => 1297599113 [1] => a:3:{i:0;s:3:"bla";i:
1;s:3:"bla";i:2;s:3:"bla";} )

What i want:
INSERT INTO `sessions`.... WHERE session_id
=gos81vu0ss9tl8tui959knrdo5 ...

What on earth should I do to make this work?

gabor de mooij

unread,
Feb 14, 2011, 2:18:46 AM2/14/11
to redbe...@googlegroups.com
I will look at it today asap

Verstuurd vanaf mijn iPhone

gabor de mooij

unread,
Feb 14, 2011, 3:24:36 PM2/14/11
to redbe...@googlegroups.com
I am sorry Jay, but what you want is beyond the scope of RedBeanPHP.

You can format the id of bean but Redbean always requires an
auto-incremented id. The only way to accomplish what you want is:

$sessionObject = R::dispense("sessions");
$sessionObject->session_id = 'gos81vu0ss9tl8tui959knrdo5';
$sessionObject->expire = (time() + 3600);
$sessionObject->session_data = serialize( array('bla','bla','bla') );

$id = R::store( $sessionObject );

But you will always get the 'extra' auto-incremented ID. There is no
way around that.
It may sound inflexible but I have to make choices.

Cheers
Gabor

Jay

unread,
Feb 14, 2011, 4:23:04 PM2/14/11
to redbeanphp
Hi Gabor,

Thanks a lot for all of your help and support - very appreciated. It's
really a shame that redbean won't support this, it would definitely
increase the superb amount of "developer freedom" that redbean already
offers. I really like the fluidness of redbean and how it helps to
reduce boundaries in the developing of a web application!

If this changes in the future - I'll be the first one to use RedBean
as the default ORM in my future projects :)


Keep up the good work!

Regards,
Jay

On 14 Feb, 21:24, gabor de mooij <gabordemo...@gmail.com> wrote:
> I am sorry Jay, but what you want is beyond the scope of RedBeanPHP.
>
> You can format the id of bean but Redbean always requires an
> auto-incremented id. The only way to accomplish what you want is:
>
> $sessionObject = R::dispense("sessions");
> $sessionObject->session_id = 'gos81vu0ss9tl8tui959knrdo5';
> $sessionObject->expire = (time() + 3600);
> $sessionObject->session_data = serialize( array('bla','bla','bla') );
>
> $id = R::store( $sessionObject );
>
> But you will always get the 'extra' auto-incremented ID. There is no
> way around that.
> It may sound inflexible but I have to make choices.
>
> Cheers
> Gabor
>
> On Mon, Feb 14, 2011 at 8:18 AM, gabor de mooij <gabordemo...@gmail.com> wrote:
>
> > I will look at it today asap
>
> > Verstuurd vanaf mijn iPhone
>

Shady Brook Software

unread,
Mar 16, 2011, 9:31:31 AM3/16/11
to redbe...@googlegroups.com
Jay,

I had the same requirement so I forked RedBeanPHP on GitHub (see https://github.com/shadybrooksoftware/redbean).  The code is, most likely, not bulletproof, but so far it has worked for the project I needed it to.  Please, let me know if you find any bugs.

Usage:

$bean = R::dispense('bean_table');
$bean->setMeta('sys.idfield', 'beanID');
$bean->setMeta('sys.iddatatype', 'varchar(128) not null');
$bean->taste = 'yummy';
R::store($bean);

For now, you have to set the "sys.idfield" and "sys.iddatatype" for every bean.

Hope it works for you!

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Shady Brook Software

unread,
Mar 16, 2011, 7:12:54 PM3/16/11
to redbe...@googlegroups.com
Gabor,

That seems to only allow changing of the PRIMARY KEY's field name, not the field data type.  I needed to work with a PRIMARY KEY that was a VARCHAR and in a previous post you mentioned that RedBean "always requires an
auto-incremented id".

If I am missing something please let me know.  And thank you for RedBean.  It is a great library.




> Date: Wed, 16 Mar 2011 21:53:47 +0100
> Subject: Re: Using RedbeanPHP without "id" column
> From: gabord...@gmail.com
> To: redbe...@googlegroups.com
>
> Why not simply use the bean formatter?
> Then you can format all beans at once.
>
> http://www.redbeanphp.com/#/Prefixes
>
Reply all
Reply to author
Forward
0 new messages