php yii从查询中获取列结果

时间:2012-09-19 11:58:23

标签: php sql yii

我已经完成了一个已经重新生成结果的查询。我不知道如何检索值。我只需要一个列值

            //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;

我如何从记录中获取id,因为它只返回1条记录

@GBD

我试过了

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,
        ));
    }

但我收到错误

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)

我想要做的是使用该记录中的唯一字段original_asset_number来检索记录的ID,id称为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 个答案:

答案 0 :(得分:2)

您可以通过以下方式获得价值:

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

但是如果你的查询总是返回一条记录,那么很高兴使用find函数

$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
}