Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Execute Ms Sql Stored Procedure

$
0
0
Hi!

I've started using Yii just a couple of days ago.
Setting up was easy. I'm still learning the whole MVC architecture.
I need to execute stored procedures from a MS SQL Server Database.

I have a model class named Marcas:
<?php

/**
 * This is the model class for table "TblMarcas".
 *
 * The followings are the available columns in table 'TblMarcas':
 * @property integer $IDMarca
 * @property string $NomeMarca
 * @property string $DenominacaoSocial
 * @property string $Morada
 * @property string $Telefone
 * @property string $Data
 * @property string $PaginaWeb
 * @property string $TextoLivre
 * @property string $DataRegisto
 * @property string $DataAlteracao
 * @property string $UserRegisto
 * @property string $UserAlteracao
 *
 * The followings are the available model relations:
 * @property TblPackdeOpcoes[] $tblPackdeOpcoes
 * @property TblMarcaDoconcessionario[] $tblMarcaDoconcessionarios
 * @property TblModelos[] $tblModeloses
 */
class Marcas extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Marcas the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'TblMarcas';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('IDMarca', 'required'),
			array('IDMarca', 'numerical', 'integerOnly'=>true),
			array('NomeMarca', 'length', 'max'=>25),
			array('DenominacaoSocial', 'length', 'max'=>100),
			array('Morada', 'length', 'max'=>200),
			array('Telefone, UserRegisto, UserAlteracao', 'length', 'max'=>20),
			array('PaginaWeb, TextoLivre', 'length', 'max'=>1073741823),
			array('Data, DataRegisto, DataAlteracao', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('IDMarca, NomeMarca', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tblPackdeOpcoes' => array(self::HAS_MANY, 'TblPackdeOpcoes', 'IDMarca'),
			'tblMarcaDoconcessionarios' => array(self::HAS_MANY, 'TblMarcaDoconcessionario', 'IDMarca'),
			'tblModeloses' => array(self::HAS_MANY, 'TblModelos', 'IDMarca'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'IDMarca' => 'Idmarca',
			'NomeMarca' => 'Nome Marca',
			//'DenominacaoSocial' => 'Denominacao Social',
			//'Morada' => 'Morada',
			//'Telefone' => 'Telefone',
			//'Data' => 'Data',
			//'PaginaWeb' => 'Pagina Web',
			//'TextoLivre' => 'Texto Livre',
			//'DataRegisto' => 'Data Registo',
			//'DataAlteracao' => 'Data Alteracao',
			//'UserRegisto' => 'User Registo',
			//'UserAlteracao' => 'User Alteracao',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('IDMarca',$this->IDMarca);
		$criteria->compare('NomeMarca',$this->NomeMarca,true);
		//$criteria->compare('DenominacaoSocial',$this->DenominacaoSocial,true);
		//$criteria->compare('Morada',$this->Morada,true);
		//$criteria->compare('Telefone',$this->Telefone,true);
		//$criteria->compare('Data',$this->Data,true);
		//$criteria->compare('PaginaWeb',$this->PaginaWeb,true);
		//$criteria->compare('TextoLivre',$this->TextoLivre,true);
		//$criteria->compare('DataRegisto',$this->DataRegisto,true);
		//$criteria->compare('DataAlteracao',$this->DataAlteracao,true);
		//$criteria->compare('UserRegisto',$this->UserRegisto,true);
		//$criteria->compare('UserAlteracao',$this->UserAlteracao,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
            'defaultOrder'=>'NomeMarca ASC',
        ),
        'pagination'=>array(
            'pageSize'=>50
        ),
		));
	}

	public function showMarcas()
	{
		$marcas=array();
		$connection=Yii::app()->db;

		$result =$connection->createCommand("exec usp_pn_GetMarcas_Web")->queryColumn();
		foreach($result as $record)
		{
		array_push($marcas,$record['NomeMarca']);
		}

		return $marcas;
	}
}


My controller MarcasController:
class MarcasController extends Controller
{
	public function actionIndex()
	{
		$marcas = Marcas::model()->showMarcas();
		$this->render('index', array('model' => $marcas));
	}
}


And my view index.php
<?php
/* @var $this MarcasController */

$this->breadcrumbs=array(
	'Marcas',
);
?>
<h1><?php echo $this->id . '/' . $this->action->id; ?></h1>

&lt!--<p>
	You may change the content of this page by modifying
	the file <tt><?php echo __FILE__; ?></tt>.
</p>-->
<p>
	<?php 
		
		$marcas = Marcas::model()->showMarcas();
		$count_marcas = count($marcas);
		if($count_marcas > 0)
		{
			foreach($marcas as $marca) 
			{
				echo $marca->NomeMarca. '<br/>';
			}
		}
		
	?>
</p>



But nothing is displayed.

Can somebody please give me some input as to what I'm doing wrong?

Thank you very much.
Mário

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>