Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to create a user that the two tables would be affected
  • I have two tables the user table and the student table, in my user table I have username, email, firstname, lastname, middlename while in my student table I have "course". How do I add in user table that the course table will also be affected. 

    I get this error: 
    Form instance already exists, cannot be recreated. Use instance() instead of forge() to retrieve the existing instance.


  • I assume that your user and student table have a one-to-many relation? Or is this a one-to-one?

    And what do you exactly want? You want to create a single form which contains fields from both tables?
  • Yup exactly I want to create a single form which contains fields form both tables :)
  • My user contains many user like admin, parent and student, student have unique entity and that is course
    I wan't to connect its id to course. So that when I add a student user it will be added and view in student table 
  • HarroHarro
    Accepted Answer
    If you use a fieldset, you can use the add_model() method to add the fields of a second model in one go. That might not work well, for example if you have duplicate field names.

    Alternatively, you can use add() to add individual fields to it.

    And if it's a one-to-many, you can use the "tabular form" feature of the fieldset. That will give you a screen that is split in two: the top has a form for the parent model, and the bottom has rows for the child models. This for example allows you do generate a view for a user and all the courses the user has subscribed to. Note that is hasn't got pagination support, so only do this for a limited number of child rows.
  • I also did that but something doesn't work well, that's why my last choice is to use Orm relation and to join the two, but I don't know how to use it. 
  • Whether or not two tables are related doesn't have any impact on generating fieldsets, so that has no impact.

    Can you post both models and your controller on http://bin.fuelphp.com so I can see what you are trying to do?
  • thanks a lot bro, I really appreciate your time and effort that you give to all the one who ask :)
    I salute you sir!
  • Thanks.

    About the relation: can a user have multiple student records (i.e. multple courses), or is a student only at one at the time?
  • The student can only have one course, yup :)
  • Is it possible that the user_id in my course will not be inputted but it will auto generate when I add user?
    what way is it possible?
  • HarroHarro
    Accepted Answer
    I must say I am very confused about your code, is this a copy from something else? I see code like

            $data['student'] = Model_User::find('all', array('related' => array('users')));

    which doesn't make sense at all, not to mention the fact that your models doesn't define any relations. So we have to fix that first: http://bin.fuelphp.com/snippet/view/Nc

    This will allow you to do:

    $user = Model_User::find(1):
    echo $user->student->course;

    or

    $student = Model_Student::find(1);
    echo $student->user->username;

    I am also confused by the fact that you seem to have two controller that partly do the same thing. Then you seem to to have users, which may be parents, may be students, but I don't see any relation between parents and students (which seems logical). Also, I see multiple methods doing the same thing?
  • HarroHarro
    Accepted Answer
    If you use relations, foreign keys will be added automatically.

    $newuser = Model_User::forge();
    $newuser->username = 'bla';

    $useruser->student = Model_Student::forge();
    $useruser->student->course = "PHP 101";

    $newuser->save();

    This save will automatically also create the student record, and populate the foreign key. (when using the models I just posted).
  • You save my life Bro, your the best, hope you will save more lives.
    This kind of people keeps me motivate and love web development,
    stay strong bro, many people need you. :) 
  • Always happy to help.

    Just paying back for all the people that helped me when I was a novice. ;-)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion