CSV导出包括来自其他表的行

时间:2013-12-19 08:53:40

标签: php mysql sql cakephp

我正在尝试导出到CakePHP中的CSV,因为我是Cake的新手,请与我一起出去,并且被要求在短时间内执行此操作。

基本上我遇到的问题是,数据库被拆分为多个表。 一个用于产品,一个用于艺术家,一个用于用户。

除了艺术家的电子邮件地址存储在用户表中之外,这一切都很好,我希望这些包含在导出中。我已经尝试添加列(csv)的相应标题,然后添加     $结果['用户'] ['用户名&#39], 可以在下面的完整功能中看到的行,但是它将电子邮件字段留空。请帮忙!

function export($event_id)
{
    $this->layout = 'blank_layout';

    ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large

    //create a file
    $filename = "export_".date("Y.m.d").".csv";
    $csv_file = fopen('php://output', 'w');

    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="'.$filename.'"');

    $results = $this->Sculpture->find('all', array('conditions' => array('event_id' =>       $event_id), 'order' => 'artist_id'));

    // The column headings of your .csv file
    $header_row = array(
        "Artist",
        "Email",
        "Sculpture", 
        "Materials", 
        "Description",
        "Price",
        "Notes"
        );
    fputcsv($csv_file,$header_row,',','"');

// Each iteration of this while loop will be a row in your .csv file where each field corresponds to the heading of the column
    foreach($results as $result)
    {
    // Array indexes correspond to the field names in your db table(s)
        $row = array(
        $result['Artist']['name'],
        $result['Users']['username'],
        $result['Sculpture']['title'],
        $result['Sculpture']['materials'],
        $result['Sculpture']['description'],
        $result['Sculpture']['price'],
        $result['Sculpture']['notes'],
    );

    fputcsv($csv_file,$row,',','"');
    }

    fclose($csv_file);
}

雕塑模型

    <?php
App::uses('AppModel', 'Model');
/**
 * Sculpture Model
 *
 * @property Artist $Artist
 * @property Event $Event
 */
class Sculpture extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
    'artist_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'event_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'title' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Please tell us the name of your sculpture',
            'allowEmpty' => true,
            'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'materials' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Please tell us the materials used in this sculpture',
            'allowEmpty' => true,
            'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'price' => array(
        'money' => array(
             'rule'    => array('numeric'),
            'message' => 'Please include the price of your sculpture.'
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    )
);

public function beforeValidate()
{
    //$p = $this->data['Sculpture']['price'];

    // Get decimal place if available
    //$dec = substr(, $start)

    $this->data['Sculpture']['price'] = preg_replace("/[^0-9.]/", '', $this->data['Sculpture']['price']);
    return true;
}
//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
public $belongsTo = array(
    'Artist' => array(
        'className' => 'Artist',
        'foreignKey' => 'artist_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Event' => array(
        'className' => 'Event',
        'foreignKey' => 'event_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);


/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
public $hasAndBelongsToMany = array(
    'MediaFile' => array(
            'className' => 'MediaFile',
            'joinTable' => 'sculptures_media_files',
            'foreignKey' => 'sculpture_id',
            'associationForeignKey' => 'media_file_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
    )
);

}

新错误报告

$this->loadModel('Artist');
    $artist = $this->Artist->find('first', array('conditions' => array('Artist.user_id' =>        $this->Auth->user('id'))));
    $this->set('artist_id', $artist['Artist']['id']);
EventsController::view() - APP/Controller/EventsController.php, line 78
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 92

来自雕塑模型的新新错误     $ results = $ this-&gt; Sculpture-&gt; find(&#39; all&#39;,array(&#39;条件&#39; =&gt;数组)&#39; event_id&#39; =&gt; $ event_id),&#39; order&#39; =&gt;&#39; artist_id&#39;,&#39; recursive&#39; =&gt; 2));

    debug($results); die();

新错误     $这 - &GT; loadModel(&#39;艺术家&#39);     $ artist = $ this-&gt; Artist-&gt; find(&#39; first&#39;,array(&#39; conditions&#39; =&gt; array)&#39; Artist.user_id&#39; =&gt; ; $ this-&gt; Auth-&gt; user(&#39; id&#39;))));     $ this-&gt; set(&#39; artist_id&#39;,$ artist [&#39;艺术家&#39;] [&#39; id&#39;]);     EventsController :: view() - APP / Controller / EventsController.php,第78行     ReflectionMethod :: invokeArgs() - [internal],line ??     Controller :: invokeAction() - CORE / Cake / Controller / Controller.php,第486行     Dispatcher :: _ invoke() - CORE / Cake / Routing / Dispatcher.php,第187行     Dispatcher :: dispatch() - CORE / Cake / Routing / Dispatcher.php,第162行     [主要] - APP / webroot / index.php,第92行

/app/Controller/SculpturesController.php (行 70 ) 阵列(     (int)0 =&gt;阵列(         &#39;雕塑&#39; =&GT;阵列(             &#39; ID&#39; =&GT; &#39; 462&#39 ;,             &#39; artist_id&#39; =&GT; &#39; 1&#39 ;,             &#39; EVENT_ID&#39; =&GT; &#39; 1&#39 ;,             &#39;标题&#39; =&GT; &#39;&#39 ;,             &#39;材料&#39; =&GT; &#39;&#39 ;,             &#39;描述&#39; =&GT; &#39;&#39 ;,             &#39;版&#39; =&GT; &#39;&#39 ;,             &#39;价格&#39; =&GT; &#39; 0.00&#39 ;,             &#39;缸&#39; =&GT;假,             &#39; media_file_id&#39; =&GT; &#39; 0&#39 ;,             &#39;输送&#39; =&GT; &#39;二零一三年十二月十六日&#39 ;,             &#39;笔记&#39; =&GT; &#39;&#39 ;,             &#39;创建&#39; =&GT; &#39; 2013-12-16 12:52:14&#39;,             &#39;改性&#39; =&GT; &#39; 2013-12-16 12:52:14&#39;         )         &#39;艺术家&#39; =&GT;阵列(             &#39; ID&#39; =&GT; &#39; 1&#39 ;,             &#39;名称&#39; =&GT; &#39; Amanda Noble&#39;,             &#39;网站&#39; =&GT; &#39; www.thefusedgarden.co.uk&#39 ;,             &#39;材料&#39; =&GT; &#39;玻璃和不锈钢&#39;,             &#39;位置&#39; =&GT; &#39; NORTHANTS&#39 ;,             &#39;创建&#39; =&GT; &#39; 2013-04-30 14:53:25&#39;,             &#39;改性&#39; =&GT; &#39; 2013-07-09 15:21:53&#39;,             &#39; USER_ID&#39; =&GT; &#39; 11&#39 ;,             &#39;用户&#39; =&GT;阵列(                 &#39;密码&#39; =&GT; &#39;的 * &#39 ;,                 &#39; ID&#39; =&GT; &#39; 11&#39 ;,                 &#39;用户名&#39; =&GT; &#39; noblept@aol.co.uk' ;,                 &#39; ROLE_ID&#39; =&GT; &#39; 4&#39 ;,                 &#39;创建&#39; =&GT; &#39; 2013-07-01 15:26:40&#39;,                 &#39;改性&#39; =&GT; &#39; 2013-07-01 15:26:40&#39;             )             &#39;雕塑&#39; =&GT;阵列(                 (int)0 =&gt;阵列(                     &#39; ID&#39; =&GT; &#39; 138&#39 ;,                     &#39; artist_id&#39; =&GT; &#39; 1&#39 ;,                     &#39; EVENT_ID&#39; =&GT; &#39; 2&#39 ;,                     &#39;标题&#39; =&GT; &#39;仲夏蓝&#39;,                     &#39;材料&#39; =&GT; &#39; Glass&amp;不锈钢&#39;,                     &#39;描述&#39; =&GT; &#39; 3个熔融玻璃面板中的一个。作为单个面板或作为一组3&#39;出售,                     &#39;版&#39; =&GT; &#39;&#39 ;,                     &#39;价格&#39; =&GT; &#39; 144.00&#39 ;,                     &#39;缸&#39; =&GT;假,                     &#39; media_file_id&#39; =&GT; &#39; 0&#39 ;,                     &#39;输送&#39; =&GT; &#39; 2013年7月28日&#39 ;,                     &#39;笔记&#39; =&GT; &#39;当以三件套出售时,价格降低10%。 我们将到达上午,但不需要协助安装我们的雕塑&#39;,                     &#39;创建&#39; =&GT; &#39; 2013-07-09 15:06:56&#39;,                     &#39;改性&#39; =&GT; &#39; 2013-07-09 15:21:07&#39;                 )                 (int)1 =&gt;阵列(                     &#39; ID&#39; =&GT; &#39; 159&#39 ;,                     &#39; artist_id&#39; =&GT; &#39; 1&#39 ;,                     &#39; EVENT_ID&#39; =&GT; &#39; 2&#39 ;,                     &#39;标题&#39; =&GT; &#39;蓝色的晚上&#39;,                     &#39;材料&#39; =&GT; &#39;玻璃和不锈钢Seel&#39;,                     &#39;描述&#39; =&GT; &#39; 3个熔融玻璃面板中的一个作为单个面板或一组3个出售(另外2个是仲夏蓝色和蓝色阴影)&#39;,                     &#39;版&#39; =&GT; &#39;&#39 ;,                     &#39;价格&#39; =&GT; &#39; 128.00&#39 ;,                     &#39;缸&#39; =&GT;假,                     &#39; media_file_id&#39; =&GT; &#39; 0&#39 ;,                     &#39;输送&#39; =&GT; &#39; 2013年7月29日&#39 ;,                     &#39;笔记&#39; =&GT; &#39;每个面板都是独一无二的,因为任何一块熔融玻璃都不会相同,但可以创建一个类似但不完全复制的任何面板。 显示的图像具有相似的图像,但实际的展品仍处于创作过程中,图像不可用。&#39;,                     &#39;创建&#39; =&GT; &#39; 2013-07-09 15:30:53&#39;,                     &#39;改性&#39; =&GT; &#39; 2013-07-09 16:31:17&#39;                 ),

1 个答案:

答案 0 :(得分:0)

如果您正在遵循蛋糕规定,那么它应该是

$result['User']['username'],

(型号名称必须是单数而不是复数)

在看到Scuplure模型文件后编辑:

因为User与Artist Model相关(而不是直接与Sculpture Model相关),您必须在find调用中设置'recursive'参数:

$results = $this->Sculpture->find(
    'all', 
    array(
        'conditions' => array('event_id' => $event_id), 
        'order' => 'artist_id',
        'recursive' => 2
    )
);