Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release

[eluser]m4rw3r[/eluser]
Seems like a really awesome behaviour for IR!
Slugs are always nice from a search engine optimization perspective + a user can easier remember the url.

That with data from related tables, it is not that hard (but it may need some diverse config options to be able to get the right column/row).
You can use the save_pre_strip_data to fetch the related column and then store it internally in the behaviour (to use when you want to).

[eluser]Muser[/eluser]
I've updated the behaviour and now labels from related columns are permitted.

Model
Code:
class Vehicle extends IgnitedRecord {
    var $belongs_to = array('vehiclebrand','vehicletype');
    var $has_many = 'vehiclephotos';
    var $act_as = array(
        'sluggable' =>
            array(
                'label' => array( array('vehiclebrand' => 'name'), 'name','year') ,
                'overwrite_on_update' => TRUE
            ),
        'timestamped');
}

Controller
Code:
function add()
    {
        $this->output->enable_profiler = TRUE;

        $brand = $this->vehiclebrand->find(5);
        $type = $this->vehicletype->find(1);

        $vehicle = $this->vehicle->new_record(
        array(
            'name' => 'Prius',
            'year' => '2005'
        )
        );

        $vehicle->add('vehiclebrand',$brand);
       // Two save calls (1st slug: 'prius-2005') (2nd slug: 'toyota-prius-2005')
        $vehicle->add('vehicletype',$type);
    }


But there is a problem. The slug is created for every save() call, and for the first save() call (vehicle save call) related('relation')->get() returns FALSE. So, the slug is created by only table columns' labels.

Check this:

http://code.google.com/p/muser/source/br...ggable.php

[eluser]cayasso[/eluser]
Is there an easy way to set up self-related relationship in IR, so for example I have a table called Pages, A page can have related records in the same table related by a fk called parent_id.

So I have page "About Us" with id 1 as primary key but also have 2 more child pages with there own id but with parent id 1 because they should have the same content as they parent but in different languages.

PARENT
id: 1
title: About Us
language: en
resource_id: 0

CHILDS
id: 2
title: Acerca de
language: es
parent_id: 1

id: 3
title: Translated title
language: fr
parent_id: 1

How can I set this type of relationship thing with IR?

Thank you in advance Smile

JB

[eluser]sofbas[/eluser]
Hi, I have just started to use IR 1.0pre and I have to say it is excellent.

I have a question to ask, and apologize if it has been answered before.

I have 2 models: Application and Status where
Application has:
status_id
...

Status has:
id,
name

Code:
class Application extends IgnitedRecord {
    var $belongs_to = 'status';
}

class Status extends IgnitedRecord {
    var $has_many = 'applications';
}

Controller
Code:
function index()
{
    $data['applications'] = $this->application->find_all();
    $this->template->write_view('content', 'admin/application', $data);
    $this->template->render();
}

View
Code:
<h3>List of Applications</h3>
&lt;? if ($applications) { ?&gt;
<table>
<tr><th>Name</th><th>Status</th></tr>
&lt;? foreach ($applications as $a): ?&gt;
    <tr>
        <td>&lt;?= $a->name ?&gt;</td>
        <td>
            &lt;? /*********************************/
            $this->db->where('id', $a->status_id);
            $query = $this->db->get('status');
            if ($query->num_rows() > 0)
            {
                $row = $query->row();
                echo $row->name;
            }  /*********************************/ ?&gt;
        </td>
    </tr>
&lt;? endforeach; ?&gt;
</table>
&lt;? } else { ?&gt;
    <p>No records</p>
&lt;?  } ?&gt;

I am trying to display the name of the Status for each Application record. How can I use IgnitedRecord to do it simply, using the relations?

$a is an IR_record and I tried using $a->related('status')->get() and I get an error.

Thanks

[eluser]sophistry[/eluser]
i do not think that you are mapping your data model correctly.

it looks to me like you need the belongs_to relation going the other direction... as in:

status belongs to application

that is, if status_id is the foreign key in the applications table then that means that the status of the application "belongs to" the application.

does that sound right to you?

[eluser]sofbas[/eluser]
Yes that sounds right to me and it is how I see it logically, but in the IR user guide: IgnitedRecord User Guide › The Has Many Relationship shows it to be the other way around.

Normally it makes more sense the way it has been documented, but in the case of the status, it doesn't. Question is, how do I implement it?

[eluser]sophistry[/eluser]
also, a has_many in status does not make sense unless there is a foreign key in there - and you only list id and name as columns in the status table. i think you need to try out different relations and or data model.

BTW, i usually put a join table into a relationship like this. it just makes things better in the long run to have the data model work before you start adding ORM.

so, status would be its own table, applications its own table and they join in a third table called application_status which has two FKs. to give an application a status, you just make a join record. this is a has and belongs to many relationship.

cheers.

[eluser]sofbas[/eluser]
The tables I have shown have quite a number of columns pruned, as they weren't relevant to the issue I raised. Nevertheless, why would I need to use a join table into this relationship, as it is normalised (at least I think so for the future as well)?

I can see how having a join table will work nicely, and actually will make more sense in terms of a has_many relation, but isn't that an issue with the ORM, rather than the data model itself?

Hmmm come to think of it I have previously used this relation as a join table, but the reasons are beyond me for this moment. Appreciate your input.

[eluser]m4rw3r[/eluser]
@sofbas:
There are no errors in your data model, nor is it an error in IgnitedRecord.

The problem is this: singular('status') -> "statu".
So it is an error made by CI's inflector helper.

What you can do is to define the column name yourself, to avoid the error:
Code:
var $belongs_to = array('name' => 'status', 'fk' => 'status_id');

[eluser]sofbas[/eluser]
Excellent, that fixed it, thank you very much.

Now to get the name I just have to add the following to the view.

View
Code:
<td>&lt;?= $a->related('status')->get()->name; ?&gt;</td>

Much simpler and cleaner. Love this library.




Theme © iAndrew 2016 - Forum software by © MyBB