magento 数据库 sql 不工作

magento database sql not working

所以不知道为什么这个功能不起作用?我正在尝试 select 来自 table 的所有 ID,但没有 selected.

       public function jobsArray()
      {
        $connection = Mage::getSingleton('core/resource')->getConnection('Envato_CustomConfig_Job');
        $result = $connection->fetchAll("SELECT id FROM Envato_CustomConfig_Job");

        $rows = array();
        foreach($result as $record) {
            $rows = ('value'=>$record, 'label'=>$record);
        }
        return $rows;
      }

下面这个函数工作正常,我需要上面的函数来做下面的函数。

  public function toOptionArray()
  {
    return array(
      array('value'=>1, 'label'=>'one'),
      array('value'=>2, 'label'=>'Two'),
      array('value'=>3, 'label'=>'Three'),            
      array('value'=>4, 'label'=>'Four')                     
    );
  }

您的代码有几个问题:

您只选择了一个项目(id,但稍后,我假设您需要一个 ID 和一个值)。

    $result = $connection->fetchAll("SELECT id FROM Envato_CustomConfig_Job");

record 是来自您的 SQL 查询的数组,因此您应该这样对待它。例如。 $record['id']

$rows 你想要一个数组,但你每次都覆盖它,所以 $rows[] = 更有意义

类似于:

public function jobsArray()
{
    $connection = Mage::getSingleton('core/resource')->getConnection('Envato_CustomConfig_Job');
    $result = $connection->fetchAll("SELECT id, label FROM Envato_CustomConfig_Job");

    $rows = array();
    foreach($result as $record) {
        $rows[] = array('value'=>$record['id'], 'label'=>$record['label']);
    }
    return $rows;
}

尝试使用核心 read/write 资源。变化

$connection = Mage::getSingleton('core/resource')->getConnection('Envato_CustomConfig_Job');

$connection = Mage::getSingleton('core/resource')->getConnection('core_read');