我正在尝试将以下查询转换为数据提供程序,以便它可以显示在 CGridView 中。我曾尝试使用 CArrayDataProvider,但到目前为止还没有任何运气,任何帮助将不胜感激!
这是查询
public function getTeamsByLevelIdAndCompetitionId($levelId, $competitionId)
{
$query = "SELECT t.*,
(SELECT COUNT(*)
FROM tbl_competition_teams ct
WHERE ct.team = t.id
AND ct.competition = :competitionId) AS 'inCompetition'
FROM tbl_teams t
WHERE t.level = :levelId";
$params = array(
'levelId' => $levelId,
'competitionId' => $competitionId
);
$result = array();
$teams = $this->findAllBySQL($query, $params);
return $teams;
}
Run Code Online (Sandbox Code Playgroud)
这就是我尝试将其放入 CArrayDataProvider 的方式:
public function getTeamsByLevelIdAndCompetitionId($levelId, $competitionId)
{
$rawData = Yii::app()->db->createCommand("SELECT t.*,
(SELECT COUNT(*)
FROM tbl_competition_teams ct
WHERE ct.team = t.id
AND ct.competition = …Run Code Online (Sandbox Code Playgroud) 我刚刚在控制器中执行了一些代码,并尝试找到最后插入的id.
但它显示错误:
这是我的代码:
$sql = 'INSERT into "Tbl_Community" ("User_id","Community_name") VALUES (10,'new community')';
$connection = Yii::app() -> db;
$command = $connection -> createCommand($sql);
$command -> execute();
echo $connection->getLastInsertID();
Run Code Online (Sandbox Code Playgroud)
错误:
[message:protected] => SQLSTATE[42602]: Invalid name: 7 ERROR: invalid name syntax
[string:Exception:private] =>
[code:protected] => 42602
[file:protected] => D:\wamp\www\Tiein\framework\db\CDbConnection.php
[line:protected] => 548
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => D:\wamp\www\Tiein\framework\db\CDbConnection.php
[line] => 548
[function] => lastInsertId
[class] => PDO
[type] => ->
[args] => Array
(
[0] =>
)
)
Run Code Online (Sandbox Code Playgroud) 我正在 yii 框架中读取 csv 文件。它遍历文件中的每个单词,但只保存最后一个单词。例如,我的 csv 文件中有以下单词。
First
Second
Third
Fourth
Run Code Online (Sandbox Code Playgroud)
以下是我遍历文件的代码。
$fileHandler=fopen("upload.csv",'r');
if($fileHandler){
while($line=fgetcsv($fileHandler,1000)){
$model->image_url=$line[0];
$model->save();
}
}
Run Code Online (Sandbox Code Playgroud)
它只是在我的数据库中保存值“第四”。请指导。
我需要为我的 yii 驱动的项目中的不同模块创建通用对话框小部件,表单将自动序列化并传递给控制器操作以进行添加/更新操作。
如果我在我的小部件的重载 run() 或 init() 方法中调用渲染部分方法并使用 html 表单向他传递自定义视图路径(此表单将在 dialog-btn 单击操作后显示在模态对话框窗口中),这是一个好习惯吗? )?
我的小部件路径:
/protected/components/widgets/DialogFormWidget.php
我的表单路径:
/protected/modules/module/controller/views/forms/submittable_html_form.php
我在 Yii 中启用了 CSRF 验证:
'enableCsrfValidation' => true,
一切都按预期工作,但是我希望会话 cookiesecure开启该标志。
使用其他 cookie,您可以在配置中设置安全标志:
'session'=>array(
'cookieParams' => array(
'httponly'=>true,
'secure' => true,
),
),
Run Code Online (Sandbox Code Playgroud)
你是如何做到这一点的YII_CSRF_TOKEN?
我正在使用 Yii 框架开发数据库应用程序。我正在从 MySQL 数据库中读取表并将它们显示给用户。我需要用户能够过滤表中的字段或搜索某个值。
例如,我有一个名为“supermarkets”的表:
CREATE TABLE IF NOT EXISTS `supermarkets` (
`Name` varchar(71) NOT NULL,
`Location` varchar(191) DEFAULT NULL,
`Telephone` varchar(68) DEFAULT NULL,
`Fax` varchar(29) DEFAULT NULL,
`Website` varchar(24) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)
.../型号/超市:
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Supermarkets extends ActiveRecord
{
}
Run Code Online (Sandbox Code Playgroud)
.../views/supermarkets/index.php:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Supermarkets</h1>
<ul>
<?php
$array = (array) $supermarkets;
function build_table($array){
// start table
$html = '<table class="altrowstable" id="alternatecolor">';
// header row
$html .= '<tr>';
foreach($array[0] …Run Code Online (Sandbox Code Playgroud) 我们正在开发一个 Yii 网络应用程序,在本地笔记本电脑上设置它后,我们将项目推送到 git 存储库(没有vendor目录)。现在我想把它拉到另一台本地机器上,但是当我尝试这样做时,供应商目录当然不存在并且我收到一些错误(例如,访问 index.php 时出现空白页,运行要求时出错。 php...)。
我应该也推送供应商目录还是为每台本地机器下载 yii2 框架更好?在第二种情况下,我应该怎么做,因为我们之前已经使用 Composer 创建了项目并且我们不想再次调用它?或者有没有更好的方法来用 git 处理 Yii 项目?
我是 yii2 的初学者。我想显示帖子创建的时间。我使用波纹管函数来获取它,但结果只是0 分钟前。谁能帮我?
<?php
function notifyDate($myStartDate) {
$now = Yii::$app->jdate->date('Y/m/d') . '- ' . date('H:i:s');
$datediff = $now - $myStartDate;
if ($datediff < (60 * 60)) { // Minutes
return floor($datediff / (60 * 60 * 24)) . " Minutes ago ";
}
if ($datediff < (60 * 60 * 24)) { // Hours
return floor($datediff / (60 * 60 * 24)) . " Hours ago ";
}
// this return the number of day
return floor($datediff …Run Code Online (Sandbox Code Playgroud) 我有这个:
<?php
use app\models\Location;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Role;
?>
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'roleId', Yii::$app->formtemplate->fieldTemplate())->dropDownList(ArrayHelper::map(Role::find()->all(), 'id', 'name'), array('prompt' => '-- Select a role --', 'class' => 'form-control select2')); ?>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'published')->checkbox(['label' => ''], true)->label($model->getAttributeLabel('published'), ['class' => 'form-label semibold']); ?>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我认为这是低效的,因为我必须将模板添加到每个字段(我知道我可以为每个表单添加它,但是复选框有一个不同的,任何想法如何为所有表单全局设置它?不仅仅是一个表单?
到目前为止我所做的是创建一个名为FormTemplate的组件,以避免直接在视图中编写模板,这很好,但我想全局设置它。
<?php
namespace app\components;
use Yii;
use yii\base\Component;
class FormTemplate extends Component {
public function fieldTemplate($option = []) {
$template = [
'template' …Run Code Online (Sandbox Code Playgroud)