Categories > TinyButStrong general >

Working with ORM

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: buhrmi
Date: 2008-05-31
Time: 04:47

Working with ORM

Hi there,

I consider using TBS for a new project. But I have serious concerns using it with ORM (ie. like propel).

Consider i have propel setup for table that has users and addresses (1:n relation), and i like to iterate over the users living together with an other user. propel provides functions like $user->getAddress()->getUsers()->getName().

In my own engine i would write
{foreach var=$user.address as=$address}
    Street: {echo var=$address.street} <br />
    {foreach var=$address.users as=$otheruser}
        {echo var=$otheruser.name} <br />
    {/foreach}
    <br />
{/foreach}

This can be done by only having to provide the $user variable to my template engine. In TBS however, it seems i cannot do this, without doing some funy magic with some %p1% parameter for subblocks. Very disturbing. Any hints for me, please?

Thanks,
Stefan
By: Skrol29
Date: 2008-06-01
Time: 16:08

Re: Working with ORM

Hi,

You can use grouping too.
See the example page.
By: buhrmi
Date: 2008-06-02
Time: 06:12

Re: Working with ORM

I love the way TBS works with HTML analysis and wysiwyg support, but I still dont get this.

In a traditional template engine one can use nested foreach tags to iterate over multidimensional arrays. Maybe it takes time till my brain adapts to the  concept TBS is using, but i cant think of any equally elegant way of doing it. I dont see how grouping helps me... if i have nested arrays, eg:
$users = array(
    "Stefan" => array(
        "Nicknames" => array(
            "Ste123" => array(
                "type" = "public",
                "active" = true
               )
             )
         )
     )
[assume multiple recoreds in each array]

Can somebody provide me with a concrete example of how to iterate over all users, and their nicknames, and print them each inside a div if their type is public
(pseudo template engine code)
foreach $users:
    foreach $nicknames:
       if ($nickname.type="public") <div> $user . $nickname </div>

help appreciated ;)
stefan
By: Skrol29
Date: 2008-06-03
Time: 00:09

Re: Working with ORM

Hi Stefan,

Your second snippet cannot be done with simple TBS functions.

The goal of the template engine is not to do anything with it, but only to separate design from the coding. TBS gives the functions to merge data with a simple structure. It has also primitive if/then/else solutions, but it does not go so far in this way. If the data to merge comes from a more complicated structure, or with more complicated conditions, then you have to "prepare" your data in the PHP side before to merge them. In you case it can be done using a simple PHP array having items which are  references to the original data.

If one specific structure become common to be merged, then you can code a piece custom of plug-in for them. Database plug-in are made for that purpose and they don't need so much line to code.

I'm surprised that you are disturbed with the subblock feature. Maybe it is because of the manual. Subblocks are not specific to TBS, you can meet them in other template engines. For example, Smarty has a plug-in for subblocks. Subblocks should not be seen has a way to merge structured data. They are a way to merge a block using a link to another parent block. Because the key of the link is given by the parent data, it has to be expressed and passed to the subblock data source. That is the meaning of the keyword %p1%. Keyword %p1% is the hook which binds the parent data source with the subblock data source.

I hope my answer will help :)
By: buhrmi
Date: 2008-06-03
Time: 10:17

Re: Working with ORM

Hey,

yeah it helps and I understand. But its a shame it does not go further in this direction, cause I really like the way TBS is taking as a template engine.

Maybe if there is time i will give it a shot and try to add/rewrite some parts of the whole block/p1/thingy to make it work more elegantly with nested datasets, for example an multidimensional array or ORM objects.

How cool would it be if one could do something like this?
<div class="userscontainer">
    [users;block=div]  (start the parent-block)
    [users.username]  (echo out a value of the parent record)
    <div class="nicknamescontainer">
        [users.nicknames;block=div]  (start sublock)
        [users.nicknames.name]        (echo out a value of the sub record)
    </div>
</div>

The syntax probably does not make sense (how should TBS know if users.nicknames is a subblock or a value to display), but i believe you get what I mean.
You only would have to merge the parent dataset (users), and everything else would be merged too.

Well... Maybe someday, in my dreams, this will become reality ;)
By: Skrol29
Date: 2008-06-03
Time: 12:53

Re: Working with ORM

Hi Stefan,

You idea for a kind of "automatic subblocks" is a very nice suggestion.
I can see yet several difficulties, but nothing impossible. The main difficulty  is when you give this feature, users will want more. I have already such a problem with the feature that enables you to specify an array to merge using a kind of query for arrays.
Example:
$TBS->Merge('n,'array','users[1][nickanmes]');
As you can see this feature is very useful for subblocks. In your case you could use it like this:
$TBS->Merge('u,'array',$users);
$TBS->Merge('n,'array','users[%p1%][nickanmes]');
This is cool, but now users ask for WHERE statements to select items in the arrays. Unfortunately creating a small database engine for PHP array is quite complicated to code and goes away from the main purpose of the template engine.

Back to your automatic subblock idea:
As you said, you must use a different separator for automatic subblocks. For example, it can be: [user:nicknames.name]
This is not difficult to code, and can be easily done with a plug-in if you thing you will need it often. You're welcome to submit such a plug-in.
My fear is that your feature will call for improvements. Users will need IF/WHERE feature also, just like you suggested previously on 2008-06-02 (same thread).