Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    •  
      CommentAuthorTalizmelf
     

    Hi there!
    I have a new issue...
    I want to save a new record of a master table and the detail tables at the same time...
    I get my records saved on each table but the records on the detail tables does not save the foreign id (the id of the record in the master table).
    I do this only by calling a form_helper like:

    <%= text_field "master", "field_name" %>
    <%= text_field "detail1", "field_name" %>
    <%= text_field "detail2", "field_name" %>
    

    If there is another option that I have to pass by parameter for the akelos to make the association between tables (in the db server) or is there another way please somebody let me know...

    I'll Appreciate it...

    •  
      CommentAuthorTalizmelf
     

    Guess we find for a record in a model and we wanted to display it in a view using form_helpers...
    So far I know form_helpers are referenced to an attribute of a model and automaticly look for the data in the the model located as an attribute of the controller.

    When the form is submited, their values are saved into the params attribute and ss long as models instances do not keep data between actions, the data is just automaticly setted again in the model for the next action...
    Note that the model now is taken as a new record unless I've also submited the record Id with a hidden_field for example or with the url and then controller have the same name of our model (I only know these two ways, if there is another, let me know)...

    Doing it one of these way, in my next action I have form_helpers to show the model but I also have form_helper to show the associated models of our model...
    Of course they're not showed automaticly, I found a way to load them by calling:

    $this->model->associated_model->load();  
    

    But the models are loaded as member of the main model and as they're not located as members of the controller, they're not showed by the view with the form_helpers..
    I kindda solve it by assigning the references of it to a member of the controller but
    I wish to know if there if a way,

    • First for the associations to be load automaticly.
    • Second for they to be loaded as members of the controller instead of the main model.
    • And third whether it's possible to keep data in models between actions instead of finding again the same record in every action...

    Thanks a lot... I really really apreciate any help...

    •  
      CommentAuthorTalizmelf
     

    Nobody knows how to help me??????????????
    please.........

    • CommentAuthorinsanet
     

    for the first question, you can define something like this:

     $has_many = array(   'comments'  => array('include' => 'author'));
    

    and it will be load automatically. for the third question, mmmm i dont think there is a method for that,(would be great thought), maybe serialize and store in session?.

    •  
      CommentAuthorTalizmelf
     

    mmmm.... thanks a lot... I'll try it

    •  
      CommentAuthorTalizmelf
     

    I didn't get it... you meant define that where??

    because if it is in the model, it does not work, besides it doesn't make any sense, I mean, wich model would be this, comments? author?

    please.. I don't get it...

    What does kaste or bermi says??

    •  
      CommentAuthorTalizmelf
     

    I have checked the whole ak files (AkActiveRecord, AkAssociatedActiveRecord, AkActionController, etc.) I found nothing...

    Just a few tricky ways, nothig I believe is well done, nothing without repeating code and I needed to make it general, automatic and reuseable...

    It looks like I'm gonna end writing and steping on akelos find or something like this to achieve what I need...

    Thanks anyway anyone....

    • CommentAuthorKaste
     
    • First for the associations to be load automaticly.

    In the controller you can define

    var finder_options=array();
    

    and this will result in

    $this->Model->find(:id,:finder_options);
    

    I don't like this and I dont use it. For this to work (mainly scaffolding I would say) you need to pass an :id via the url or params as you guessed. But when you have various model to instantiate it's easy to mismatch the id's (for each model) and the finder_options would be the same etc. So just don't.

    As far as I understand you need something smart that fulfills your needs, which means you should code it.

    That doesn't mean code-dupes, because you can share code with a proper useage of inheritance, filters and helpers, I think.

    • Second for they to be loaded as members of the controller instead of the main model.

    Just define var $models='model1,model2';

    • And third whether it's possible to keep data in models between actions instead of finding again the same record in every action...

    store'em in teh SESSION,

    but why?

    •  
      CommentAuthorTalizmelf
     

    Hi...
    I realize what you said first before... That's one of the things I meant when I said tricky ways.
    with declaring:
    public $models = "model1,model2";

    they're instantiated but not loaded, only the model with the same name that the controller o the ones who's id is sent in the request (like a hidden field or somethig... another tricky way).
    I was planing to code this thing as you said, but I was asking looking for some akelos structure help. After being stumbling around and even studying part of the akelos source code, I decided that the only way I could make it is overwriting the find method for the ActiveRecord...
    And for last, I wanted to know whether it's possible to keep data because in most cases, the model should keep the same record then this will be very usefull to avoid to keep finding the same record over and over...

    Thanks a lot Kaste...

    • CommentAuthorKaste
     

    Actually you can't predict what a user will request next. He can follow one of the links on your page, go back and forth. There's just a certain probabillity he will click on exactly the link ('next') you zhink he should.

    On the other side with multiple users a data-set can change at any time (unless you lock).

    So this must be well thought out and match exactly your domain.

    Last: If you find useful, generalized 'patterns', then publish and we'll discuss.

    •  
      CommentAuthorTalizmelf
     

    Ehmm I did not understand exactly what you meant...
    I believed a server releases a thread or something of the application for each user, I did not think this.. You have raised me a good doubt...
    I'll learn about it...

    For the last thing... I will make a try and if my code works I'll let you guys know...

    Thanks again... later...

    •  
      CommentAuthorTalizmelf
     
    The web server is the one who is in charge of handling the requests asigning one thread to each user requesting...

    It got nothing to deal with keeping things loaded between actions...
    They're a few ways to do it. One is through sessions in php...
    What I was asking is whether the framework already has some stuff for that manipulation...

    No problem with that, I can code it...
    •  
      CommentAuthorTalizmelf
     

    One queston, What do:

    $this->Mymodel->loadAssociations();
    

    ?? thanks...

    • CommentAuthorKaste
     

    loadAssociations() instantiates the associations you defined as class-variables. Usually called automatically.

    •  
      CommentAuthorTalizmelf
     
    ehmm... can you specify this a little bit more?
    Wich are the ones I do define as class-variables?? I believe I'm not doing such a thing :p
    where is the place to do that??
    and where are the associations instantiated??

    thanks...
    •  
      CommentAuthorTalizmelf
     

    Hey what's up guys!!...

    I have a question, this is urgent... I need to know something...
    I'll start with an example.

    I know so far, when I use akelos helper for a text field for example something like this is rendered:

    <%= text_field "model", "column" %>
    <input type="text" id="model_column" name="model[column]">
    

    in the view is showed the data refered to the controller var when we loaded it:

    $this->model->column
    

    Also we know as far when a model has associations, one of the attributes of the model can be another model itself (the associated model),
    if I do this in the view:

    <%= text_field "associatedmodel", "associatedmodelcolumn" %>
    

    what's showed in the view is refered to:

    $this->associatedmodel->associatedmodelcolumn
    

    And that's not what I wanted... Is there ANY POSSIBLE way to refer that to:

    $this->model->associatedmodel->associatedmodelcolumn
    

    PLEASE, I need to kown this, it's the better way I see to be able to link models to show and save their data..
    Thanks a lot!!!

    • CommentAuthorinsanet
     

    in fact this:(if this is what you thought exist)

    name="model[assioactedmodel][column]" // or something like this
    

    doenst exist/ not implemented / not even in RoR.

    so i think you should go back to:

      <%= text_field "associatedmodel", "associatedmodelcolumn" %>
    

    yes, there is ways to save associated model, doing like this, been there. tell us how your models are associated.

    •  
      CommentAuthorTalizmelf
     
    I don't understand what you've tried to say..
    Thanks anyway...
    •  
      CommentAuthorTalizmelf
     
    Please bermi.. help!
    •  
      CommentAuthorbermi
     

    An associated model attribute will not be set by you automatically using the helpers and the setAttributes() on the parent model. As insanet said, you'll have to use html for the form field, some singtags/php for the value and then loop results on the controller or create a method on the parent model.

    <input type="text" id="parent_associate_attribute" name="parent[associate][attribute]" value="{parent.associate.attribute?}" />
    
    •  
      CommentAuthorTalizmelf
     
    mmm.. let me clarify..
    - there is no way to generate this input using helpers?
    - the input won't show automatically the content in $this->model->associatedmodel->column ??
    - I will not be able to set the attributes by the method setAttributes() ??

    please tell me, and if it is like this, get me close to where I can modify this, I mean, wich method of akelos carry out this process so I can take a look at there and see if is there a chance to overwrite it or something...

    please....
    •  
      CommentAuthorTalizmelf
     
    Bermi.. let me clarify that please...
    •  
      CommentAuthorbermi
     
    • there is no way to generate this input using helpers?

    Not with existing ones, but you can write your own.

    the input won't show automatically the content in $this->model->associatedmodel->column ??

    Using my previous example it should

    I will not be able to set the attributes by the method setAttributes() ??

    You can use:

    $this->model->associatedmodel->setAttributes();
    

    after loading the association using

    $this->model->associatedmodel->load();
    

    or a finder include option.

    please tell me, and if it is like this, get me close to where I can modify this, I mean, wich method of akelos carry out this process so I can take a look at there and see if is there a chance to overwrite it or something...

    You can create your own helpers if you want at app/helpers

    Core helpers are located at /lib/AkActionView/helpers

    And a compiled to PHP version of the templates can be found at AK_TMP_DIR

    •  
      CommentAuthorTalizmelf
     
    Thank you much bermi!!! :)
    •  
      CommentAuthorTalizmelf
     
    Seriously... what does loadAssociations() do??
    Because it is doing nothig to me. How to use it??
    I asked it before, but I didn't get clear of what Kaste told me...
    Please somebody??
    •  
      CommentAuthorTalizmelf
     
    yuju!
    •  
      CommentAuthorTalizmelf
     

    Now this happen, I have this model with a foreing key, and I do:

    $this->model->setAttributes(array('foreigntable_id'=>$number));
    

    And when I do save(), does not work.
    With other fields it does but not with the foreing key field

    • CommentAuthorKaste
     

    The foreigntable_id_column is kinda locked because it is assumed that you should use your associations f.i. assign an ActiveRecord-Object instead of 'brute-force-assigning' the right foreign_id.

    loadAssociations() is called at the end of the constructor. it looks for your defined associations (f.i. var has_many = 'questions';), instantiates a 'HasMany-Association' and therefore actually 'expands' the base ActiveRecord so you can query Person->question... and Person->questions.

    •  
      CommentAuthorTalizmelf
     
    It looks wery well kaste, kind of as I imagined it should be...
    But I didn't get very well where to place those things... Let me try and I tell you later..
    •  
      CommentAuthorTalizmelf
     

    Ok... I'll show you an example with a main model and some associations and then you tell me..
    These are the models for the example:
    - Person (this is also the controller name)
    - City (Person belongs to City)
    - Course (Person has many Courses)

    I tried this:

    $this->Person->setAttributes($this->params['person']);
    $this->Person->City = new City ($this->params['person']['city_id']);
    foreach ($this->params['person']['courses'] as $i => $course){
        if (isset($this->Person->courses[$i])){
            $this->Person->courses[$i]->setAttributes($course);
        }
        else{
            $this->Person->courses[$i] = new Course($course);
        }
    }
    $this->Person->save();
    

    Trying to follow that you told me... This worked (cool).. But...
    - is this the way to do it?
    - is this what you meant? Or..
    - is there another way to do it wich would be the correct way???

    Another thing... I still don't know how to use loadAssociations() :( I don't understand where to call this method............

    Thank you so much kaste..

    •  
      CommentAuthorTalizmelf
     

    kaste please..

    •  
      CommentAuthorTalizmelf
     

    Can you tell me??

    • CommentAuthorKaste
     

    Ok, this is complex stuff. First you don't need loadAssociations(). It's used internally, except if you hack yourself into Akelos.

    I have some concerns about the has_many relation. I just don't know how this should work.

    If you add a new course, you can simply

    $this->Person->course->add(new Course...); 
    

    To update an existing course, you need the id of that course (or another unique column). From the given code I assume that $i is an integer without further meaning. It's just an index in an array. You get that index from the user input. So it's not predictible.

    $person->courses also holds an array. But the order of that array is in fact not predictable [if you don't use an explicit order in your SQL or finder, which you don't regarding your code.]

    A solution in pseudo-code could be

    iterate over params['person']['courses'] as $course
        //$courses will be an array of ActiveRecord-objects
        $courses[] = find_and_update_or_create ($course)  //a method you have to code which returns an ActiveRecord, a Course, for a given user-input
    finally $this->Person->course->set($courses); //this should also remove a course just in case
    

    Ok, thats not super easy to get, I think.

    •  
      CommentAuthorTalizmelf
     

    mmm...
    well, yes, $Person->courses is an array with ActiveRecord objects and $this->params['person']['courses'] is an array of sets of asttributes for a Course object. I didn't know how setting and saving the data for an associated model works, so you tell me I had to set it an instance of the associated model, and I guessed (as what I coded) you meant to append a new instance of the associated model to the collection of associated when I was going to save the main model, this for the new records, and for the existing ones, the model is already instantiated so I should only set the attributes.
    That's what I assumed and that's why I was asking...
    now we stay a bit in the sames :P
    For what you suggested, I coded some methods in shared_model wich can do something like this and they can work as something general and reusable...
    I would show them to you if you like to... you tell me the way (shall I post it here or...)

    Another thing, I still don't get really well how those add and set work (what you coded: course->add(new Course..) and course->set($courses))..
    I mean, I don't find them in the api for AkAssociatedModel...
    Can you explain them to me??

    And for the last, I guess you're right and loadAssociations() is used internally because it do nothing when I call it..
    I didn't assume it because it's not a privated method...

    well, Thanks so much kaste...

    • CommentAuthorKaste
     

    You find some docs in lib/AkActiveRecord/AkAssociations/AkHasMany.php .

    You might like to study the testcases, because tests are always usecases. ->test/unit/lib/AkActiveRecord/AkHasMany.php (though esp. these tests aren't that well-coded, 'pretty')

    herein you find:

    //given: a Property :has_many Pictures
    $Property = new Property;
    
    $SeaViews = new Picture(array('title'=>'Sea views'));
    $Property->picture->add($SeaViews); //adds one object to the collection
    $Property->save();
    
    $Picture = new Picture; //thats a php-hack, because we can't statically call Picture::find('all')
    $Pictures = $Picture->find('all');  
    $Property->picture->set($Pictures);  // sets a collection, 'replacing' old one
    

    hope it helps

    •  
      CommentAuthorTalizmelf
     

    Thank you man...

    •  
      CommentAuthorTalizmelf
     

    I got another situation!!!
    How do I set a foreign key for a table but it can't be setted as tablename_id...
    for example, I have these models named Person and City, a normal association would be:

    //Person
    public $belongs_to = "city";
    
    //City
    public $has_many = "people";
    

    This will look for a field named city_id in Person but Person has two fields wich should be referenced to City: born_city and living_city...
    How can I associate the models?

    •  
      CommentAuthorTalizmelf
     

    I already know how to do it..
    I found it in the wiki

    •  
      CommentAuthorTalizmelf
     

    Is there any way to do queries like:

    select * from table1 where id in (select xxx_id from table2 where some_condition);
    

    Using Akelos Active Records???

    • CommentAuthorinsanet
     
    see my post: http://forum.akelos.org/discussion/645/count-with-group-by-condition/#Item_2

    it applies for any sql query.
    •  
      CommentAuthorTalizmelf
     

    Yes, right... but I meant with active records without using sql...
    I also want to know there is some kind of lastquery() method wich returns the sql of the last query...
    Thanks...

    • CommentAuthorinsanet
     
    ah i see. well i dont know =).
    about getting the query, there was a config constant, (used for debugging) that echo all the sql querys from the beggining of the script to the end. is not exactly what you want but it would very helpful. but i dont remember it and lost it.
    •  
      CommentAuthorTalizmelf
     

    :) thanks a lot any way!!