Add PHP function in CakePHP model array - php

I have an array of behaviour $actas.Problem is when I adding date() function on the array string,it's return an error:
Example:
public $actas = array(
'Uploader.Attachment'=>array(
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/files/uploads/books' . date('d-m-Y'),//this is where I want to add the function.
'transportDir' => ''
)
)
);
however it's not working. I also do like this:
public $actas = array(
'Uploader.Attachment'=>array(
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => "/var/www/html/apps/webroot/files/uploads/books'".date('d-m-Y')."'",//this is where I want to add the function.
'transportDir' => ''
)
)
);
also not worked.
So my question is how to do that? If I have a lot of mistaken,please tell me so I can learn more about the matter.
Thanks in advance.
This is the full source code of Post.php model
<?php
App::uses('AppModel', 'Model');
/**
* Post Model
*
* #property Tier $Tier
* #property Category $Category
* #property Comment $Comment
*/
class Post extends AppModel {
//var $now = 'CURDATE()';
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'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
),
),
'content' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'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
),
),
);
//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(
'Tier' => array(
'className' => 'Tier',
'foreignKey' => 'tier_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* #var array
*/
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'post_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $actsAs = array(
//'Containable',
'Uploader.Attachment' => array(
// Do not copy all these settings, it's merely an example
'banner' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => "/var/www/html/apps/webroot/img/banners/",
'transportDir' => '',
'finalPath' => '/img/banners/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
),
'feature' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/img/features/',
'transportDir' => '',
'finalPath' => '/img/features/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
),
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/files/uploads/books' . date('d-m-Y'),
'transportDir' => '',
'finalPath' => '/files/uploads/books/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
)
)
);
}

ahh ok I was not looking at it with my OO hat on. you can't do this because:
Properties
Class member variables are called "properties". You may also see them
referred to using other terms such as "attributes" or "fields", but
for the purposes of this reference we will use "properties". They are
defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. This declaration may
include an initialization, but this initialization must be a
constant value--that is, it must be able to be evaluated at compile
time and must not depend on run-time information in order to be
evaluated.
you need to use the __construct() method

Related

CakePHP bake all unknown issue

I'm new to Cake php.
I have a problem using bake
I set up a migration with user table
public $migration = array(
'up' => array(
'create_table' => array(
'users' => array(
'user_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
'username' => array('type' => 'string', 'null' => false, 'length' => 250),
'password' => array('type' => 'text', 'null' => false),
'created' => array('type' => 'string', 'null' => false, 'length' => 14),
'modified' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 14),
'indexes' => array(
'PRIMARY' => array('column' => 'user_id', 'unique' => 1)
)
)
)
),
'down' => array(
'drop_table' => array(
'users'
)
)
);
and migrate this file on the db then I tried to do the command "cake bake all"
the problem is that User made a reference to itself with belongsTo and hasMany
is that the default of using bake?
class User extends AppModel {
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'user_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
),
),
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'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
),
),
'date_created' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'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
),
),
);
//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(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* #var array
*/
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
is there something I missing here?
Should I leave that and modify it manually or there's a config that I missed.
There are three easy solutions, as the last answer stated cake bake all is following default cake conventions and creating associations by the fact that you have a user_id in the table
Either rename the database field from user_id to id and run the shell again.
Run cake bake and manually select model and configure the database associations manually with the shell helper
Manually remove the associations in the model, and the references in the controller and views.
It's correct.
When you use the cake bake all it automatically adds references to your table by convention. As your table refers to herself he identified the 'hasMany' and 'belongsTo' relationships. You should remove the link that doesn't exist or change that is generated by default. Reference: baking custom projects

CSV Export including row from other table

I am trying to export to a CSV in CakePHP.
Basically the problem that I am having is, the database is split down into multiple tables.
One for a product, one for artists and one for users.
It is all working well apart from the fact that the Email addresses for the artists are stored in the users table, I would like these to be included in the export. I have tried adding in the appropriate header for the column (csv) and then adding the
$result['Users']['username'],
Row that can be seen in my full function below, however it leaves the Email fields blank. Please help!
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);
}
Sculpture Model
<?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' => ''
)
);
}
NEW Error report
$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
NEW NEW ERROR From Sculpture Model
$results = $this->Sculpture->find('all', array('conditions' => array('event_id' => $event_id), 'order' => 'artist_id', 'recursive' => 2));
debug($results); die();
The New error
$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
<div class="cake-debug-output">
<span><strong>/app/Controller/SculpturesController.php</strong> (line <strong>70</strong>)</span>
<pre class="cake-debug">
array(
(int) 0 => array(
'Sculpture' => array(
'id' => '462',
'artist_id' => '1',
'event_id' => '1',
'title' => '',
'materials' => '',
'description' => '',
'edition' => '',
'price' => '0.00',
'vat' => false,
'media_file_id' => '0',
'delivery' => '2013-12-16',
'notes' => '',
'created' => '2013-12-16 12:52:14',
'modified' => '2013-12-16 12:52:14'
),
'Artist' => array(
'id' => '1',
'name' => 'Amanda Noble',
'website' => 'www.thefusedgarden.co.uk',
'materials' => 'Glass and stainless steel',
'location' => 'Northants',
'created' => '2013-04-30 14:53:25',
'modified' => '2013-07-09 15:21:53',
'user_id' => '11',
'User' => array(
'password' => '*****',
'id' => '11',
'username' => 'noblept#aol.co.uk',
'role_id' => '4',
'created' => '2013-07-01 15:26:40',
'modified' => '2013-07-01 15:26:40'
),
'Sculpture' => array(
(int) 0 => array(
'id' => '138',
'artist_id' => '1',
'event_id' => '2',
'title' => 'Midsummer Blue',
'materials' => 'Glass & Stainless Steel',
'description' => 'One of 3 fused glass panels. sold either as a single panel or as a set of 3',
'edition' => '',
'price' => '144.00',
'vat' => false,
'media_file_id' => '0',
'delivery' => '2013-07-28',
'notes' => 'When sold as a set of three the price is 10% lower.
We will arrive AM, but will not require assistance to install our sculptures',
'created' => '2013-07-09 15:06:56',
'modified' => '2013-07-09 15:21:07'
),
(int) 1 => array(
'id' => '159',
'artist_id' => '1',
'event_id' => '2',
'title' => 'Blue evening',
'materials' => 'Glass and Stainless Seel',
'description' => 'One of 3 fused glass panels sold either as a single panel or as a set of 3 ( the other 2 are Midsummer Blue and Blue Haze )',
'edition' => '',
'price' => '128.00',
'vat' => false,
'media_file_id' => '0',
'delivery' => '2013-07-29',
'notes' => 'Each panel is unique as no piece of fused glass will ever be identical, but it is possible to create a similar, but not exact replica of any panel.
The images shown are of a similar sets, but the actual exhibits are still in the process of creation and an image is not available.',
'created' => '2013-07-09 15:30:53',
'modified' => '2013-07-09 16:31:17'
),
if you are following cake namning convention it should be
$result['User']['username'],
(Model name must be singular and not plural)
edited after seeing Scuplure model file:
Because User is related to Artist Model (and not directly to Sculpture Model) you have to set 'recursive' parameter in your find call:
$results = $this->Sculpture->find(
'all',
array(
'conditions' => array('event_id' => $event_id),
'order' => 'artist_id',
'recursive' => 2
)
);

cakephp: saveAll is not working

The saveAll is not working can some one help me in finding what is the problem.I have included the controller's action, the array passed to saveAll and the model of it.
The controllers action containing the saveAll looks like the following-
public function add_customer_order() {
if ($this->request->is('post')) {
-
-
-
-
$this->CustomerOrderItem->create();
if ($this->CustomerOrderItem->saveAll($customer_order_item)) {
$this->Session->setFlash('The customer order Items has been saved successfully.', 'default/flash_success');
}
else {
$this->Session->setFlash('The customer order Items not been saved successfully.', 'default/flash_success');
}
}
}
The array $customer_order_item looks as follows-
array(
'CustomerOrderItem' => array(
(int) 0 => array(
'id' => '',
'quantity' => '400',
'unit_cost' => '77',
'pending_quantity' => '400',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '0',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '22'
),
(int) 1 => array(
'id' => '',
'quantity' => '200',
'unit_cost' => '77',
'pending_quantity' => '200',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '0',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '25'
),
(int) 2 => array(
'id' => '',
'quantity' => '400',
'unit_cost' => '77',
'pending_quantity' => '400',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '1',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '23'
),
(int) 3 => array(
'id' => '',
'quantity' => '200',
'unit_cost' => '77',
'pending_quantity' => '200',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '1',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '24'
),
(int) 4 => array(
'id' => '',
'quantity' => '200',
'unit_cost' => '77',
'pending_quantity' => '200',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '1',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '27'
)
)
)
The model looks as follows-
<?php
App::uses('AppModel', 'Model');
/**
* CustomerOrderItem Model
*
* #property CustomerOrder $CustomerOrder
* #property MasterArticle $MasterArticle
*/
class CustomerOrderItem extends AppModel {
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'customer_order_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
),
),
'master_article_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
),
),
);
//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(
'CustomerOrder' => array(
'className' => 'CustomerOrder',
'foreignKey' => 'customer_order_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MasterArticle' => array(
'className' => 'MasterArticle',
'foreignKey' => 'master_article_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
I am pretty sure it is a data issue either due to validation or null in db.
debug the contents of debug($this->request->data);
debug the invalid fields if any debug($this->Submission->invalidFields());. Make sure you call this after you have issues validates() request or saveAll()
Try
if ($this->CustomerOrderItem->saveAll($this->request->data)) {
First of all remove all the validation rules and then check again if error is still coming.if not then there must be issue with data validation rules or null value of 'customer_order_id'.

Convert to an array

I have a variable $code which contains the following data. I want the contents of $feeds_importer to an array. How can I do this?
$feeds_importer = new stdClass;
$feeds_importer->disabled = FALSE;
/* Edit this to true to make a default feeds_importer disabled initially */
$feeds_importer->api_version = 1;
$feeds_importer->id = 'feed';
$feeds_importer->config = array(
'name' => 'Feed',
'description' => 'Import RSS or Atom feeds, create nodes from feed items.',
'fetcher' => array(
'plugin_key' => 'FeedsHTTPFetcher',
'config' => array(
'auto_detect_feeds' => FALSE,
'use_pubsubhubbub' => FALSE,
'designated_hub' => '',
),
),
'parser' => array(
'plugin_key' => 'FeedsSyndicationParser',
'config' => array(),
),
'processor' => array(
'plugin_key' => 'FeedsNodeProcessor',
'config' => array(
'content_type' => 'feed_item',
'update_existing' => 0,
'expire' => '-1',
'mappings' => array(
array(
'source' => 'title',
'target' => 'title',
'unique' => FALSE,
),
array(
'source' => 'description',
'target' => 'body',
'unique' => FALSE,
),
array(
'source' => 'timestamp',
'target' => 'created',
'unique' => FALSE,
),
array(
'source' => 'url',
'target' => 'url',
'unique' => TRUE,
),
array(
'source' => 'guid',
'target' => 'guid',
'unique' => TRUE,
),
),
'input_format' => 0,
'author' => 0,
),
),
'content_type' => 'feed',
'update' => 0,
'import_period' => '1800',
'expire_period' => 3600,
'import_on_create' => 1,
);
i want the contents of feeds_importer to an array how can i do this?
$array = (array) $feeds_importer;

Exposing a module's data to Views2 using its API

I'm forking the filefield_stats module to provide it with the ability of exposing data into the Views module via the API.
The filefield_stats schema is as follow:
function filefield_stats_schema() {
$schema['filefield_stats'] = array(
'fields' => array(
'fid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Primary Key: the {files}.fid'),
'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Primary Key: the {node}.vid'),
'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The {users}.uid of the downloader'),
'timestamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The timestamp of the download'),
'hostname' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => '', 'description' => 'The hostname downloading the file (usually IP)'),
'referer' => array('type' => 'text', 'not null' => FALSE, 'description' => 'Referer for the download'),
),
'indexes' => array('fid_vid' => array('fid', 'vid')),
);
return $schema;
}
Well, so I implemented the hook_views_api() in filefield_stats.module & added a filefield_stats.views.inc file in the module's root directory, here it is:
// $Id$
/**
* #file
* Provide the ability of exposing data to Views2, for filefield_stats module.
*/
function filefield_stats_views_data() {
$data = array();
$data['filefield_stats']['table']['group'] = t('FilefieldStats');
// Referencing the {node_revisions} table.
$data['filefield_stats']['table']['join'] = array(
'node_revisions' => array(
'left_field' => 'vid',
'field' => 'vid',
),
'files' => array(
'left_field' => 'fid',
'field' => 'fid',
),
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
// Introducing filefield_stats table fields to Views2.
// vid: The node's revision ID which wrapped the downloaded file
$data['filefield_stats']['vid'] = array(
'title' => t('Node revision ID'),
'help' => t('The node\'s revision ID which wrapped the downloaded file'),
'relationship' => array(
'base' => 'node_revisions',
'field' => 'vid',
'handler' => 'views_handler_relationship',
'label' => t('Node Revision Reference.'),
),
);
// uid: The ID of the user who downloaded the file.
$data['filefield_stats']['uid'] = array(
'title' => t('User ID'),
'help' => t('The ID of the user who downloaded the file.'),
'relationship' => array(
'base' => 'users',
'field' => 'uid',
'handler' => 'views_handler_relationship',
'label' => t('User Reference.'),
),
);
// fid: The ID of the downloaded file.
$data['filefield_stats']['fid'] = array(
'title' => t('File ID'),
'help' => t('The ID of the downloaded file.'),
'relationship' => array(
'base' => 'files',
'field' => 'fid',
'handler' => 'views_handler_relationship',
'label' => t('File Reference.'),
),
);
// hostname: The hostname which the file has been downloaded from.
$data['filefield_stats']['hostname'] = array(
'title' => t('The Hostname'),
'help' => t('The hostname which the file has been downloaded from.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
// referer: The referer address which the file download link has been triggered from.
$data['filefield_stats']['referer'] = array(
'title' => t('The Referer'),
'help' => t('The referer which the file download link has been triggered from.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
// timestamp: The time of the download.
$data['filefield_stats']['timestamp'] = array(
'title' => t('Download Time'),
'help' => t('The time of the download.'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
return $data;
} // filefield_stats_views_data()
According to the Views2 documentations this should work as a minimum, I think. But it doesn't! Also there is no error of any kind, when I come through the views UI, there's nothing about filefield_stats data. Any idea?
I think your problem is in the function name: hook_views_data(), it should be filefield_stats_views_data().
hook_views_api() should also be filefield_stats_views_api().
You always replace hook with your module name when implementing them in your own modules.
Missing field definitions in the code above and also wrong hook_views_api() implementation. A working API implementation example can be found here: http://drupalcode.org/sandbox/sepehr/1073868.git/tree/refs/heads/master:/modules/sms_panel_views

Categories