findOrCreate return parameter

24 views
Skip to first unread message

Andreas Lenhardt

unread,
Jun 7, 2020, 8:33:54 AM6/7/20
to redbeanphp
Hello,

what does the findOrCreate function return?
If the entry is found, it probably returns the bean, but does it return null, empty array or the id if the entry is created and stored?

Ben Major

unread,
Jun 15, 2020, 2:13:23 AM6/15/20
to redbeanphp
It always returns the bean. Why would it return anything other than that, surely the whole point is to ensure your var is a reference to a bean?

Masaki Kawabata Neto

unread,
Jun 15, 2020, 2:13:23 AM6/15/20
to redbe...@googlegroups.com
Hi Andreas,
It does exactly what it says: If you search for a bean using certain criteria, if it do not exists, it will be created and the bean is returned.
Consider this code:

R::setup();
R::nuke();
R::store(R::dispense([
'_type' => 'person',
'name' => 'John Doe',
]));
$person = R::findOrCreate('person', ['name'=>'John Doe']);
print_r($person->export());
$person = R::findOrCreate('person', ['name'=>'Jane Doe']);
print_r($person->export());

Searching john doe it will return id=1 because already exists.
Searching jane doe it will return id=2 because it do not exists and it was created.

--------------
Escola de Engenharia de São Carlos - EESC - USP
Departamento de Engenharia de Estruturas - SET
Laboratório de Informática e Mecânica Computacional - LIMC
16 3373-9453


--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redbeanorm/64acffdf-27f6-4515-a345-eb82732fcd92o%40googlegroups.com.

Andreas Lenhardt

unread,
Jun 15, 2020, 4:54:31 AM6/15/20
to redbeanphp
Of course it makes sense that a new bean is created :)
I thought, maybe the function also gives me information about whether a bean has been loaded or if a new bean was created.
This dinstinction would be very handy in my opinion.

Masaki Kawabata Neto

unread,
Jun 15, 2020, 7:34:06 AM6/15/20
to redbe...@googlegroups.com
Hi, doesn't make sense to me to use this function and an if statement to check the type of return. For this distinction I
use R
::find() first and R::dispense() if necessary. The findOrCreate is most like a shortcut if this distinction is not necessary. That is true in many cases for me at least. 

Attn.

--------------
Escola de Engenharia de São Carlos - EESC - USP
Departamento de Engenharia de Estruturas - SET
Laboratório de Informática e Mecânica Computacional - LIMC
16 3373-9453

--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages