View Full Version : Tag Cloud Portlet
>@>mehr
چهارشنبه 07 خرداد 1393, 14:22 عصر
سلام من بعد از ساختن Tag Cloud Portlet
هر کاری که میخوام بکنم این ارور میاد
The table "{{tag}}" for active record class "Tag" cannot be found in the database.
components/TagCloudPortlet
کد PHP:
php?>
Yii::import(’zii.widgets.CPortlet’);
class TagCloud extends CPortlet {
public $title=’Tags’;
public $maxTags=20;
protected function renderContent() {
$tags=Tag::model()->findTagWeights($this->maxTags);
foreach($tags as $tag=>$weight) {
} }
}
postcontroller
کد PHP:
/**
* Suggests tags based on the current user input.
* This is called via AJAX when the user is entering the tags input.
*/
public function actionSuggestTags()
{
if(isset($_GET['q']) && ($keyword=trim($_GET['q']))!=='')
{
$tags=Tag::model()->suggestTags($keyword);
if($tags!==array())
echo implode("\n",$tags);
}
}
>@>mehr
چهارشنبه 07 خرداد 1393, 14:31 عصر
<?php
class Tag extends CActiveRecord
{
/**
* The followings are the available columns in table 'tbl_tag':
* @var integer $id
* @var string $name
* @var integer $frequency
*/
/**
* Returns the static model of the specified AR class.
* @return CActiveRecord 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 '{{tag}}';
}
/**
* @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('name', 'required'),
array('frequency', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>128),
);
}
/**
* @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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'name' => 'Name',
'frequency' => 'Frequency',
);
}
/**
* Returns tag names and their corresponding weights.
* Only the tags with the top weights will be returned.
* @param integer the maximum number of tags that should be returned
* @return array weights indexed by tag names.
*/
public function findTagWeights($limit=20)
{
$models=$this->findAll(array(
'order'=>'frequency DESC',
'limit'=>$limit,
));
$total=0;
foreach($models as $model)
$total+=$model->frequency;
$tags=array();
if($total>0)
{
foreach($models as $model)
$tags[$model->name]=8+(int)(16*$model->frequency/($total+10));
ksort($tags);
}
return $tags;
}
/**
* Suggests a list of existing tags matching the specified keyword.
* @param string the keyword to be matched
* @param integer maximum number of tags to be returned
* @return array list of matching tag names
*/
public function suggestTags($keyword,$limit=20)
{
$tags=$this->findAll(array(
'condition'=>'name LIKE :keyword',
'order'=>'frequency DESC, Name',
'limit'=>$limit,
'params'=>array(
':keyword'=>'%'.strtr($keyword,array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')).'%',
),
));
$names=array();
foreach($tags as $tag)
$names[]=$tag->name;
return $names;
}
public static function string2array($tags)
{
return preg_split('/\s*,\s*/',trim($tags),-1,PREG_SPLIT_NO_EMPTY);
}
public static function array2string($tags)
{
return implode(', ',$tags);
}
public function updateFrequency($oldTags, $newTags)
{
$oldTags=self::string2array($oldTags);
$newTags=self::string2array($newTags);
$this->addTags(array_values(array_diff($newTags,$oldTags) ));
$this->removeTags(array_values(array_diff($oldTags,$newTa gs)));
}
public function addTags($tags)
{
$criteria=new CDbCriteria;
$criteria->addInCondition('name',$tags);
$this->updateCounters(array('frequency'=>1),$criteria);
foreach($tags as $name)
{
if(!$this->exists('name=:name',array(':name'=>$name)))
{
$tag=new Tag;
$tag->name=$name;
$tag->frequency=1;
$tag->save();
}
}
}
public function removeTags($tags)
{
if(empty($tags))
return;
$criteria=new CDbCriteria;
$criteria->addInCondition('name',$tags);
$this->updateCounters(array('frequency'=>-1),$criteria);
$this->deleteAll('frequency<=0');
}
}
>@>mehr
چهارشنبه 07 خرداد 1393, 14:33 عصر
تیبل تگ هم اینگونه ساخته شده
CREATE TABLE tag
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
frequency INTEGER DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
MMSHFE
چهارشنبه 07 خرداد 1393, 15:20 عصر
تنظیمات کامپوننت db رو از فایل config/main.php بگذارین.
>@>mehr
چهارشنبه 07 خرداد 1393, 15:25 عصر
'components'=>array( 'user'=>array( // enable cookie-based authentication 'allowAutoLogin'=>true, ),
MMSHFE
پنج شنبه 08 خرداد 1393, 12:05 عصر
نه تنظیمات کامپوننت db رو میخوام نه خود component رو. محتوای main.php رو بگذارین اصلاً
>@>mehr
پنج شنبه 08 خرداد 1393, 12:25 عصر
<?php
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR. '..',
'name'=>'Bargue',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
'modules'=>array(
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'pa$$word',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
),
),
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
/*
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
*/
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=mydb',
'emulatePrepare' => true,
'username' => 'user',
'password' => 'pa$$word',
'charset' => 'utf8',
),
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
),
);
MMSHFE
پنج شنبه 08 خرداد 1393, 20:34 عصر
خوب توی این هم مشکل خاصی به چشمم نمیاد. بقیه مدلها درست کار میکنن؟ این رو به کامپوننت db اضافه کنید ببینید درست میشه؟
'db'=>array(
...
'tablePrefix' => 'tbl_',
),
>@>mehr
پنج شنبه 08 خرداد 1393, 22:39 عصر
بله بقیه مدلها درست کار میکنند
حتی خود تگ هم درست کار میکنه، میتونم تگهای وارد شده در یک پست را نمایش بدم.
کاری که شما گفتین رو کردم بازم درست نشد
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.