Jump to content
Larry Ullman's Book Forums

Jonathon

Members
  • Posts

    1064
  • Joined

  • Last visited

  • Days Won

    55

Posts posted by Jonathon

  1. Ok,

     

    I have been looking at the framework code, it appears that you can do it with  $model->setAttribute('user_id', 1); (I haven't done logging in yet, so that's why I haven't used the $_SESSION vairables). So I just hard coded the user_id           

     

     

     

     
    public function actionUpdate($id)
        {
            $model=$this->loadModel($id);
     
            // Uncomment the following line if AJAX validation is needed
            // $this->performAjaxValidation($model);
     
            if(isset($_POST['Item']))
            {
                $model->attributes=$_POST['Item'];
                $model->setAttribute('user_id', 1);            
     
                if($model->save())
                    $this->redirect(array('view','id'=>$model->id));
            }
     
            $this->render('update',array(
                'model'=>$model,
            ));
        }
    
  2. Well it's not so much setting the variables.

     

    It's how to use them to update/create a record, for instance to controller code for updating is:

     

     
        public function actionUpdate($id)
        {
            $model=$this->loadModel($id);
     
            // Uncomment the following line if AJAX validation is needed
            // $this->performAjaxValidation($model);
     
            if(isset($_POST['Item']))
            {
                $model->attributes=$_POST['Item'];
                if($model->save())
                    $this->redirect(array('view','id'=>$model->id));
            }
     
            $this->render('update',array(
                'model'=>$model,
            ));
        }
     
    

     

    But how do you use a value that doesn't come from a form in the updating process?

     

    On a total side note. I was so shocked at how little coding was needed to populate a form and update it. Because other than fix a few inputs into select boxes. I. Did. Nothing. This actually stresses me out, like it can't be that easy. Or is that a classic primer to "Yes It Is"?

  3. Hi,

     

    I've been playing with forms. I've used some sample data and have just been updating them. I plan to have a login system (surprising I know!) so only a user can add content. One of my fields is a user_id FK field. I noticed when it came to updating it populates the user_id input field and asks for a value ( because i put it in my rules) for this.

     

    But I was looking into my model and controller and I wasn't sure how you'd go about setting an attribute  through a $_SESSION. I suspect that when I make the login system i'l;l set a $_SESSION['user_type'] and $_SESSION['user_id']  value and then planned to just confirm the existence of these and use them for insert/updating records.

     

    But in Yii, I was a bit lost as to how to do this  :wacko:

    • Upvote 1
  4. It should be where your index.php file is stored.

     

     

     
    <ifModule mod_rewrite.c>
    # Turn on the engine:
     
    RewriteEngine on
     
    # Do not perform redirects for files and directories that exist:
     
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
     
    # For everything else, redirect to index.php:
     
    RewriteRule . index.php
     
    </ifModule>
    
    • Upvote 1
  5. Hello,

     

    My feedback form works. Which is great. I wanted to have a satisfaction rating (1-10) just radio buttons. So I started to use the Yii-Bootstrap extension. (http://www.cniska.net/yii-bootstrap/) Edward you'll like this.

     

    So I have the following code: Which lies within a

    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'feedback-form',
        'enableClientValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
        ),
    )); ?>
     
     
        <?php echo $form->errorSummary($model); ?>
     
        <div class="row">
            <?php echo $form->labelEx($model,''); ?>
            <?php echo $form->textField($model,'name', array('placeholder'=>'Name')); ?>
    </div>
    // more code
    

     

     

     
        <div class="btn-group" data-toggle="buttons-radio">
            <?php echo $form->labelEx($model,'satisfaction') . "\n"; ?>
    <?php $this->widget('bootstrap.widgets.TbButtonGroup', array(
        'type' => 'info',
        'toggle' => 'radio', // 'checkbox' or 'radio'
    'buttonType' => 'button',
        'buttons' => array(
            array('label'=>'1'),
            array('label'=>'2'),
            array('label'=>'3'),
            array('label'=>'4'),
            array('label'=>'5'),
            array('label'=>'6'),
            array('label'=>'7'),
            array('label'=>'8'),
            array('label'=>'9'),
            array('label'=>'10'),
        ),
    )); ?>
     
    

     

    Lovely, the form displays properly. But I'm not sure how I can tie the input from the satisfaction rating to the form for processing. The HTML generated looks like this:

     

    <div class="btn-group" data-toggle="buttons-radio">
      <label for="FeedbackForm_satisfaction" class="required">Satisfaction <span class="required">*</span></label>
    <div class="btn-group" data-toggle="buttons-radio">
    <button class="btn btn-info" name="yt0" type="button">1</button>
    <button class="btn btn-info" name="yt1" type="button">2</button>
    <button class="btn btn-info" name="yt2" type="button">3</button>
    <button class="btn btn-info" name="yt3" type="button">4</button>
    <button class="btn btn-info" name="yt4" type="button">5</button>
    <button class="btn btn-info" name="yt5" type="button">6</button>
    <button class="btn btn-info" name="yt6" type="button">7</button>
    <button class="btn btn-info" name="yt7" type="button">8</button>
    <button class="btn btn-info" name="yt8" type="button">9</button>
    <button class="btn btn-info" name="yt9" type="button">10</button>
    </div>
    </div>
    </div>
    
    

     

    To me this part of the form seems to be void of a value input. I'd expect some kind of name="FeedbackForm[satisfaction]". So I guess I'm wondering, what am I missing here OR is it easier just to try and style the radio buttons another way?

     

    I feel like I need to make reference to $form..

     

    Thanks

     

    Jonathon

  6. Well  it is currently in my layout file. I just thought it would be cleaner HTML wise in the view to call it from another place as it's not going to be used on every page. I'll keep it where it is. I did read that clips were more appropriate for dynamic content.

     

    Thanks for getting back to me

     

    Jonathon

  7. Hi,

     

    I'm going to have some rotating banners on some of my pages. As they are only on some of my pages I didn't want to include this in my layout. I thought placing them in a clip would be a good idea and then just rendering them in the view file. Is it?

  8. I don't really follow Thomas, sorry. 

     

    If I can explain it, I just have a feedback button that appears on every page. Currently, I decided to link this to a static page (feedback.php). But i'm not sure where I should put handling the submission, validation and sending the feedback to an email address?

  9. Hi,

     

    I've read the piece on forms. I am wanting to implement a feedback button that will appear on each page. I'm just thinking as there is no model where is the ideal place for the code for it to go? The SiteController is what i'm thinking, only because it's the default controller. Just wanted some thoughts.

     

    Thanks

     

    Jonathon

  10. Hi,

     

    I have been just playing around with Yii and querying Databases. A couple of things struck me in the last few minutes. I've been trying to get all the information I want to get over to the view in one fell swoop (1 model). Which is what I think hindered my initial work.

     

    For instance this is some code from one of my controllers

     

     
        public function actionIndex($id)
        {
     
        $companyItems = Company::model()->returnCompanyItemListings($id); // This model method returns the items listed by a company
        
            $this->render('index', array(
            'model'=>$companyItems,
                  ));
        }
     
    

     

    It works fine and returns the information I want back. (It's actually a fairly complex three table join using DAO). Then ran into a similar problem as before. I can only use this information once in the foreach loop. I then remembered some code in another controller about loading models that I used. (It's actually the code for loadModel().

     

    I then updated my code in the controller to this:

    	public function actionIndex($id)
    	{
    
    	$companyItems = Company::model()->returnCompanyItemListings($id); // This model method returns the items data
    	
    	$model = Company::model()->findByPk($id);
    
    		$this->render('index', array(
    		'model'=>$companyItems,
    		'model2'=>$model,
    		));
    	}
    
    

     

     

    So by passing 2 models I can use one $model2->name to echo out the company on the company listing page and it will be dynamically adjusted. I can then use $model in the foreach loop to get the items data. Is that acceptable?? Because if so. It's been a decent discovery for me and something I hadn't put together from the book.

     

    Jonathon

  11. Hi Thomas,

     

    I am actually Jonathon  :lol:

     

    Yes, I'm going to take your approach and try that. I am doing Larry's project in the Yii book. But also I am just doing my own practice project too at the same time to practice and get more familiar with Yii. Things sound simple when read, but unless I need to do exactly what's in the book I'm not 100% sure of how to do things. So I thought this would be a good way to practice and experiment whilst having guidance through the book too. I will do my best to report back on any solutions or ideas I come across.

     

    Thanks for your help!

×
×
  • Create New...