0

I have doen a query which has retruned a result. I do not know how to retrieve the values. I only require one column value

            //print_r($_POST['Components']);
            $criteria = new CDbCriteria;
            $criteria->condition = "(original_asset_number = '". $_POST['Components']['original_asset_number'] ."')";
            $fixedAsset=FixedAsset::model()->findAll($criteria);
            print_r($fixedAsset);
            //$fixed_asset_id = $fixedAsset->fixed_asset_id;

How would I just get the id from the record, as it only would ever return 1 record

@GBD

I have tried

public function actionCreate()
    {
        $model = new Components;
        $model_fixedAsset = new FixedAsset;
        $model_comAsset = new ComAsset;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Components']))
        {
            $model->attributes=$_POST['Components'];


            // Check if asset exist and get PK
            if( $_POST['Components']['original_asset_number'] != "" ){
                //print_r($_POST['Components']);
                $criteria = new CDbCriteria;
                $criteria->condition = "(original_asset_number = :original_asset_number)";
                $criteria->params = array(":original_asset_number" =>  $_POST['Components']['original_asset_number'] );
                $fixedAsset = FixedAsset::model()->findAll($criteria);
                //print_r($model_fixedAsset);
                $id = $fixedAsset->fixed_asset_id;
                print_r("model:". $id);
            }



            /*if($model->save())
                $this->redirect(array('view','id'=>$model->component_id));*/
        }

        $this->render('create',array(
            'model'=>$model,
            'model_fixedAsset'=>$model_fixedAsset,
            'model_comAsset'=>$model_comAsset,
        ));
    }

but I get error

2012/09/19 13:49:26 [error] [php] Trying to get property of non-object (/opt/dam/protected/controllers/ComponentsController.php:85)
Stack trace:
#0 /opt/dam/yii-1.1.12.b600af/framework/web/filters/CFilterChain.php(134): ComponentsController->runAction()
#1 /opt/dam/yii-1.1.12.b600af/framework/web/CController.php(292): CFilterChain->run()
#2 /opt/dam/yii-1.1.12.b600af/framework/web/CController.php(266): ComponentsController->runActionWithFilters()
#3 /opt/dam/yii-1.1.12.b600af/framework/web/CWebApplication.php(283): ComponentsController->run()
#4 /opt/dam/yii-1.1.12.b600af/framework/web/CWebApplication.php(142): CWebApplication->runController()
#5 /opt/dam/yii-1.1.12.b600af/framework/base/CApplication.php(162): CWebApplication->processRequest()
#6 /opt/dam/index.php(13): CWebApplication->run()
REQUEST_URI=/dam/index.php?r=components/create
in /opt/dam/protected/controllers/ComponentsController.php (85)
in /opt/dam/index.php (13)

what i am trying to do is retrieve the id of a record by using a unique field within that record which is original_asset_number, id is called fixed_asset_id

print_r(model_fixed_asset);

Array ( [0] => FixedAsset Object ( [_md:private] => CActiveRecordMetaData Object ( [tableSchema] => CMysqlTableSchema Object ( [schemaName] => [name] => fixed_asset [rawName] => `fixed_asset` [primaryKey] => fixed_asset_id [sequenceName] => [foreignKeys] => Array ( ) [columns] => Array ( [fixed_asset_id] => CMysqlColumnSchema Object ( [name] => fixed_asset_id [rawName] => `fixed_asset_id` [allowNull] => [dbType] => varchar(20) [type] => string [defaultValue] => [size] => 20 [precision] => 20 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [description] => CMysqlColumnSchema Object ( [name] => description [rawName] => `description` [allowNull] => 1 [dbType] => varchar(30) [type] => string [defaultValue] => [size] => 30 [precision] => 30 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [original_asset_number] => CMysqlColumnSchema Object ( [name] => original_asset_number [rawName] => `original_asset_number` [allowNull] => 1 [dbType] => varchar(30) [type] => string [defaultValue] => [size] => 30 [precision] => 30 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [flag] => CMysqlColumnSchema Object ( [name] => flag [rawName] => `flag` [allowNull] => 1 [dbType] => tinyint(1) [type] => integer [defaultValue] => 0 [size] => 1 [precision] => 1 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [disposal_date] => CMysqlColumnSchema Object ( [name] => disposal_date [rawName] => `disposal_date` [allowNull] => 1 [dbType] => date [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) ) [_e:private] => [_m:private] => ) [columns] => Array ( [fixed_asset_id] => CMysqlColumnSchema Object ( [name] => fixed_asset_id [rawName] => `fixed_asset_id` [allowNull] => [dbType] => varchar(20) [type] => string [defaultValue] => [size] => 20 [precision] => 20 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [description] => CMysqlColumnSchema Object ( [name] => description [rawName] => `description` [allowNull] => 1 [dbType] => varchar(30) [type] => string [defaultValue] => [size] => 30 [precision] => 30 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [original_asset_number] => CMysqlColumnSchema Object ( [name] => original_asset_number [rawName] => `original_asset_number` [allowNull] => 1 [dbType] => varchar(30) [type] => string [defaultValue] => [size] => 30 [precision] => 30 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [flag] => CMysqlColumnSchema Object ( [name] => flag [rawName] => `flag` [allowNull] => 1 [dbType] => tinyint(1) [type] => integer [defaultValue] => 0 [size] => 1 [precision] => 1 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) [disposal_date] => CMysqlColumnSchema Object ( [name] => disposal_date [rawName] => `disposal_date` [allowNull] => 1 [dbType] => date [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:private] => [_m:private] => ) ) [relations] => Array ( [components] => CManyManyRelation Object ( [_junctionTableName:private] => [_junctionForeignKeys:private] => [limit] => -1 [offset] => -1 [index] => [through] => [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [together] => [scopes] => [name] => components [className] => Components [foreignKey] => com_asset(fixed_asset, component) [select] => * [condition] => [params] => Array ( ) [group] => [join] => [having] => [order] => [_e:private] => [_m:private] => ) ) [attributeDefaults] => Array ( [flag] => 0 ) [_model:private] => FixedAsset Object ( [_md:private] => CActiveRecordMetaData Object *RECURSION* [_new:private] => [_attributes:private] => Array ( ) [_related:private] => Array ( ) [_c:private] => [_pk:private] => [_alias:private] => t [_errors:private] => Array ( ) [_validators:private] => [_scenario:private] => [_e:private] => [_m:private] => ) ) [_new:private] => [_attributes:private] => Array ( [fixed_asset_id] => FA0008 [description] => Spectrum House - Exit panel [original_asset_number] => B0795 [flag] => 0 [disposal_date] => 2011-11-04 ) [_related:private] => Array ( ) [_c:private] => [_pk:private] => FA0008 [_alias:private] => t [_errors:private] => Array ( ) [_validators:private] => [_scenario:private] => update [_e:private] => [_m:private] => ) ) 

1 Answer 1

2

You can get value by following way:

 $fixedAsset=FixedAsset::model()->findAll($criteria);
 $id = $fixedAsset[0]->fixed_asset_id;

But if your query always return one record, then good to use find function

$criteria = new CDbCriteria;
$criteria->condition = "(original_asset_number = :original_asset_number')";
$criteria->params = array(
    ':original_asset_number' => $_POST['Components']['original_asset_number'],
);
$fixedAsset=FixedAsset::model()->find($criteria);
if(!is_null($fixedAsset)) {
   $id = $fixedAsset->fixed_asset_id
}
Sign up to request clarification or add additional context in comments.

3 Comments

can you paste result of print_r($fixedAsset); ?
for second one, have you used find method ?
my mistake, works now. how would i check if it returns nothing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.