Results 1 to 3 of 3

Thread: In this thread we will discuss the YII Framework!

  1. #1
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default In this thread we will discuss the YII Framework!

    Some of the discussion started in the other thread, lets hope no hijacking occurs in this one. In this thread we will ask/answer questions about the Yii framework and prove links to tutorials where necessary.

    how does it handle multiple table relationships in a single form?

  2. #2
    Join Date
    Mar 2011
    Posts
    485
    Rep Power
    0

    Default

    oh goodie. So I recently started toying with the yii framework. So far I am pretty impressed. Holding tight for the procedural crew to kill my new found framework high....

  3. #3
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Providing you have your models setup properly:

    User(id, username, password)
    Profile(user_id, first_name, last_name, gender)

    form:

    PHP Code:
    <form action="path/to/controller" method="post">
        <fieldset>
            <legend>Account Information</legend>
            <p>
                <?php echo CHtml::label($user'username'); ?>
                <?php echo CHtml::activeTextField($user'username'); ?>
            </p>
            <p>
                <?php echo CHtml::label($user'password'); ?>
                <?php echo CHtml::activePasswordField($user'password'); ?>
            </p>
        </fildset>
        <fieldset>
            <legend>Profile Information</legend>
            <p>
                <?php echo CHtml::label($profile'first_name'); ?>
                <?php echo CHtml::activeTextField($profile'first_name'); ?>
            </p>
            <p>
                <?php echo CHtml::label($profile'last_name'); ?>
                <?php echo CHtml::activeTextField($profile'last_name'); ?>
            </p>
            <p>
                <?php echo CHtml::label($profile'gender'); ?>
                <?php echo CHtml::activeDropDownList($profile'gender', array('prompt'=>'Select a gender:','m'=>'Male''f'=>'Female')); ?>
            </p>
        </fildset>
        <p>
            <input type="submit" value="Save">
        </p>
    </form>
    controller action

    PHP Code:
    public function actionSignup()
    {
        
    $user = new User;

        
    $profile = new Profile;

        
    // gets the current request
        
    $request Yii::app()->getRequest();

        
    // if User && Profile fields were submitted via a post request
        
    if(($userAttributes $request->getPost('User')) !== null && ($profileAttributes $request->getPost('Profile')) !== null)
        {
            
    // assigns the values to the model properties
            
    $user->attributes $userAttributes;
            
    $profile->attributes $profileAttributes;

            
    // begin a transaction
            
    $transaction $user->->dbConnection->beginTransaction();
            
            try
            {
                
    // save models and commit transaction
                
    $user->save();
                
    $profile->user_id $user->id;
                
    $profile->save();
                
    $transaction->commit();
            }catch(
    Exception $e)
            {
                
    // transaction fails, error is stored in $e->getMessage();
                
    $transaction->rollBack();
            }
            
    // render form view...
        
    }


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •