【问题标题】:php yii get column result from queryphp yii 从查询中获取列结果
【发布时间】:2012-09-19 11:58:22
【问题描述】:

我执行了一个返回结果的查询。我不知道如何检索这些值。我只需要一列值

            //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] => ) ) 

【问题讨论】:

    标签: php sql yii


    【解决方案1】:

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

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

    但是如果您的查询总是返回一条记录,那么最好使用查找功能

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

    【讨论】:

    • 你能粘贴 print_r($fixedAsset); 的结果吗? ?
    • 第二个,你用过find方法吗?
    • 我的错误,现在可以使用。我将如何检查它是否不返回任何内容?
    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多