我想将我的查询值放在cakephp中的数组中

时间:2014-04-23 16:04:55

标签: php arrays postgresql cakephp

很好地解释,我有一个问题是当发送数组值都被编号并命名为apararece null时。问题是当发送数组值全部编号并且名称显示为null时。右边只显示我的身份和姓名。

ID不必编号,即0 1 2 3 4 5

每个实例的id都不同11 15 19 22 12

我正在使用cakephp和postgresql

codigo:

public function add()
{
    $this->loadModel('SoyaProveedor');
    $this->loadModel('Soya');
    $this->loadModel('Grupo');
    $alluseroleaginosas = $this->Soya->query('SELECT
            users.id as users__id,
            Perfil.nombrecomercial as Perfil__nombrecomercial
            FROM
            users 
            INNER JOIN Grupo ON (Grupo.id = 5) 
            INNER JOIN Perfil ON (users.id = Perfil.user_id);');

    // Here is where you fill the array 
    $empresasoleaginoasas = array();
    foreach ($alluseroleaginosas as $oleaginosa) {
        $id = $oleaginosa['users']['id'];
        $nombre = $oleaginosa['perfil']['nombrecomercial'];
        $empresasoleaginoasas[] = $id[$nombre];
    }
    $this->set('oleaginosas', $empresasoleaginoasas);




    if ($this->request->is('post')) {
    $this->request->data['SoyaProveedor']['nombre'] = strtoupper($this->request->data['SoyaProveedor']['nombre']);
    $this->request->data['SoyaProveedor']['codigo'] = strtoupper($this->request->data['SoyaProveedor']['codigo']);
    if ($this->SoyaProveedor->save($this->request->data)) {
        $this->Session->setFlash(__('La Información fue Guardada.'));
        return $this->redirect(array('action' => 'index'));
        }
    }
}

我的观点。

<div class="soyaproductorcompras form">
 <h3>Registro de Proveedores de Soya</h3>
<?php echo $this->Form->create('SoyaProveedor');?>
    <fieldset>

        <?php 

        //Here is where I show the array
        echo $this->Form->input('user_id', array( 'options' => $oleaginosas,'empty' => '--Por favor seleccione una empresa--','label' => 'Empresas' ));



        echo $this->Form->input('nombre', array('label' => 'Nombre Completo o Razón Social del Proveedor','style'=>'width:500px; height:30px;'));



        echo $this->Form->input('cionit', array('label' => 'Ingrese su CI o NIT','style'=>'width:500px; height:30px;'));

        echo $this->Form->input('codigo', array('label' => 'Codigo (numérico o alfanumérico)','style'=>'width:500px; height:30px;'));
        echo $this->Form->input('regimen', array(
            'options' => array( 
                'GENERAL' => 'Regimen General',
                'RAU' => 'Regimen RAU'
                ), 'label'=>'Regimen Tributario'
        ));


        echo $this->Form->submit('Agregar Existencia', array('class' => 'form-submit',  'title' => 'Presione aqui para agregar datos')); 
?>
    </fieldset>
<?php echo $this->Form->end(); ?>
</div>

1 个答案:

答案 0 :(得分:2)

解决方案

我认为您的错误是:

foreach ($alluseroleaginosas as $oleaginosa) {
    $id = $oleaginosa['users']['id'];
    $nombre = $oleaginosa['perfil']['nombrecomercial'];

    // HERE YOUR ERROR
    // $empresasoleaginoasas[] = $id[$nombre];
}

你需要改变

$empresasoleaginoasas[] = $id[$nombre];

到那个

$empresasoleaginoasas[$id] = $nombre;

因为$id[$nombre]不存在。


问题

为什么使用Soya模型查询User / Perfil?

你需要改善我的朋友的代码。


约定&amp;最佳实践

关于 CakePHP约定和最佳实践我建议您不要使用query方法。

更改模型并组织关系。

模型Perfil

<?php
App::uses('AppModel', 'Model');
/**
 * Perfil Model
 */
class Perfil extends AppModel {

  // whatever you want here, like diplayField and validation

  /**
   * belongsTo associations
   *
   * @var array
   */
  public $belongsTo = array(
    'User' => array(
      'className' => 'User',
      'foreignKey' => 'user_id',
    )
  );

模型Grupo

<?php
App::uses('AppModel', 'Model');
/**
 * Grupo Model
 */
class Grupo extends AppModel {

  // whatever you want here, like diplayField and validation

  /**
   * hasMany associations
   *
   * @var array
   */
  public $hasMany = array(
    'User' => array(
      'className' => 'User',
      'foreignKey' => 'group_id',
      'dependent' => false      
    )
  );

模型用户

<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 */
class User extends AppModel {

  // whatever you want here, like diplayField and validation

  /**
   * hasMany associations
   *
   * @var array
   */
  public $hasMany = array(
    'Perfil' => array(
      'className' => 'Perfil',
      'foreignKey' => 'user_id',
      'dependent' => false      
    )
  );

然后,使用此cakephp查询例如

return $this->Perfil->find('list', array(
  'joins' => array(
    array(
      'table' => 'users',
      'alias' => 'User',
      'type' => 'LEFT',
      'conditions' => array(
        'Perfil.user_id = User.id'
      )
    ),          
    array(
      'table' => 'groups',
      'alias' => 'Grupo',
      'type' => 'LEFT',
      'conditions' => array(
        'User.grupo_id = Grupo.id'
      )
    )
  ),
  'conditions' => array(
    'Grupo.id' => 5
  ),
  'order' => array(
    'Perfil.nombrecomerical' => 'ASC'
  ),
  'recursive' => -1
)); 
  

我不知道你的表名,所以,如果不正确就改变它。


抱歉我的英文