谁一开始就是大神?

PHP 第71页

PHP

yii2中使用组件yii\widget\Menu输出的label内容带html时不会被解释,求帮助[2.0]

ACGlovely阅读(76)

直接上代码

$mainMenuItems = array();
	$mainMenuItems[] = [
		'label' => '<i class="glyphicon glyphicon-th-large"></i>首页',
		'type' => 'raw',
		'url' => ['site/index'],
		'options' => [
			'class' => ''
		],
	];

echo Menu::widget([
    'options' => [
	'class' => 'nav-tabs nav-stacked'
    ],
    'items' => $mainMenuItems
]);

如上的视图文件在页面中显示时这样的:
“&lt;i class="glyphicon glyphicon-th-large"&gt;&lt;/i&gt;首页”
而不是以html的形式显示,应该怎么办呢,知道的兄弟能否告知,不甚感激。

'type' => 'html',

执行composerrequiremdmsoft/yii2-admin"~2.0"发生错误[2.0]

loveLion阅读(81)

The "fxp/composer-asset-plugin" plugin requires composer-plugin-api 1.0.0, this *WILL* break in the future and it should be fixed ASAP (require ^1.0 for example).
PHP Fatal error:  Call to undefined method Fxp\Composer\AssetPlugin\Package\Version\VersionParser::parseLinks() in C:\Users\admin\AppData\Roaming\Composer\vendor\fxp\composer-asset-plugin\Repository\VcsPackageFilter.php on line 272

Fatal error: Call to undefined method Fxp\Composer\AssetPlugin\Package\Version\VersionParser::parseLinks() in C:\Users\admin\AppData\Roaming\Composer\vendor\fxp\composer-asset-plugin\Repository\VcsPackageFilter.php on line 272

composer global require "fxp/composer-asset-plugin:~1.1.1"
先执行这个。yiichina的文档太old了,需要升级

数据库with联合查询后怎么处理$orders[2.0]

Anitom阅读(85)

// 先执行sql: SELECT * FROM customer LIMIT 100;
// SELECT * FROM orders WHERE customer_id IN (1,2,...)
$customers = Customer::find()->limit(100)
->with('orders')->all();
foreach ($customers as $customer) {
// 在这个循环的时候就不会再执行sql了
$orders = $customer->orders;
// ...handle $orders...
}

照着文档里这么做,然后获取到的$orders是一个对象,如下:

yii\db\ActiveQuery Object
(
    [sql] => 
    [on] => 
    [joinWith] => 
    [select] => 
    [selectOption] => 
    [distinct] => 
    [from] => 
    [groupBy] => 
    [join] => 
    [having] => 
    [union] => 
    [params] => Array
        (
        )

    [_events:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => Array
        (
        )

    [where] => 
    [limit] => 
    [offset] => 
    [orderBy] => 
    [indexBy] => 
    [modelClass] => common\models\AuthAssignment
    [with] => 
    [asArray] => 
    [multiple] => 
    [primaryModel] => common\models\Admin Object
        (
            [_attributes:yii\db\BaseActiveRecord:private] => Array
                (
                    [id] => 6
                    [username] => admin
                    [password_hash] => $2y$13$oYZrIQgTsipWdcNMKWCf8uxOIS286hOif9VWxf7.xnQ31MBje5AtS
                    [auth_key] => GQF9VeroKHrdo_72dzEF6SheBBOG-cuR
                    [status] => 1
                    [created_at] => 0
                    [last_time] => 1452569448
                    [ip] => 127.0.0.1
                )

            [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
                (
                    [id] => 6
                    [username] => admin
                    [password_hash] => $2y$13$oYZrIQgTsipWdcNMKWCf8uxOIS286hOif9VWxf7.xnQ31MBje5AtS
                    [auth_key] => GQF9VeroKHrdo_72dzEF6SheBBOG-cuR
                    [status] => 1
                    [created_at] => 0
                    [last_time] => 1452569448
                    [ip] => 127.0.0.1
                )

            [_related:yii\db\BaseActiveRecord:private] => Array
                (
                    [authitem] => 
                )

            [_errors:yii\base\Model:private] => 
            [_validators:yii\base\Model:private] => 
            [_scenario:yii\base\Model:private] => default
            [_events:yii\base\Component:private] => Array
                (
                    [beforeInsert] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => yii\behaviors\TimestampBehavior Object
                                                (
                                                    [createdAtAttribute] => created_at
                                                    [updatedAtAttribute] => updated_at
                                                    [value] => 
                                                    [attributes] => Array
                                                        (
                                                            [beforeInsert] => Array
                                                                (
                                                                    [0] => created_at
                                                                    [1] => updated_at
                                                                )

                                                            [beforeUpdate] => updated_at
                                                        )

                                                    [owner] => common\models\Admin Object
 *RECURSION*
                                                )

                                            [1] => evaluateAttributes
                                        )

                                    [1] => 
                                )

                        )

                    [beforeUpdate] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => yii\behaviors\TimestampBehavior Object
                                                (
                                                    [createdAtAttribute] => created_at
                                                    [updatedAtAttribute] => updated_at
                                                    [value] => 
                                                    [attributes] => Array
                                                        (
                                                            [beforeInsert] => Array
                                                                (
                                                                    [0] => created_at
                                                                    [1] => updated_at
                                                                )

                                                            [beforeUpdate] => updated_at
                                                        )

                                                    [owner] => common\models\Admin Object
 *RECURSION*
                                                )

                                            [1] => evaluateAttributes
                                        )

                                    [1] => 
                                )

                        )

                )

            [_behaviors:yii\base\Component:private] => Array
                (
                    [0] => yii\behaviors\TimestampBehavior Object
                        (
                            [createdAtAttribute] => created_at
                            [updatedAtAttribute] => updated_at
                            [value] => 
                            [attributes] => Array
                                (
                                    [beforeInsert] => Array
                                        (
                                            [0] => created_at
                                            [1] => updated_at
                                        )

                                    [beforeUpdate] => updated_at
                                )

                            [owner] => common\models\Admin Object
 *RECURSION*
                        )

                )

        )

    [link] => Array
        (
            [item_name] => id
        )

    [via] => 
    [inverseOf] => 
)

我要怎么处理这个对象,以获取我两个数据表数据的集合,初学者求教

$customers = Customer::find()->limit(100)
->with('orders')->**asArray()**->all();

楼上正解,加上asArray()返回的就是正常的数组了

yii2的form表单样式怎么灵活控制呢?[2.0]

coolyak阅读(71)

yii2和bootstrap怎么配合,比如我用了$form->field()会自动生成一些html,其中的css是明显和bootstrap有关系的,这个时候如果我想改一下css怎么办。
例如,默认生成的是这个样子的:

QQ截图20160123084620.jpg

但是我嫌它太长了,用options加了class=col-lg-6,但是换行了
于是我用了template:

<?= $form->field($model, 'modules_name',['template'=>"<div class='col-lg-6'>{label}\n{input}\n{hint}\n{error}</div>"])->textInput() ?>

这样的话,显示正常了,但是不至于每个field都加一个template吧,有没有其他方案解决啊
或者是推荐个其他方案。

统一的样式可以在开头统一配置,个性化的设置只能每个filed单独配置。
在页面里加js代码块,直接用jquery加你想应用的样式
表示yii2的activeform非常不灵活

如何批量插入User[2.0]

Cindyseagull阅读(82)

需要批量插入学生啊,但是password字段如何搞定?

我想直接在mysql里面插入,密码是怎么加密的?有方法吗?

如果密码可以一样,你就在yii2存一条,然后拷贝出来,
如果不一样,每次都要加密,就直接在程序里操作了,不要直接在mysql了。
如果是安装了yii2-user模块,直接mysql肯定不行,通过程序也很简单:

apache配置virtualHost后。localhost无法访问www目录[2.0]

Jackbubble阅读(79)

我配置了三个virtualHost.
使用这三个vituralHost可以访问相应的应用 。现在下面三个的配置已经生效。

但是我现在想用localhost访问 www目录时,却直接被转到basic/web。换句话说就是直接显示basic应用的内容了。

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "D:/wamp/www/basic/web"
    <Directory "D:/wamp/www/basic/web">
        RewriteEngine on
        Allow from all
        # If a directory or a file exists, use the request directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Otherwise forward the request to index.php
        RewriteRule . index.php
    </Directory>
    ServerName www.yii2.com
    ServerAlias www.yii2.com
    ErrorLog "logs/yii2.com-error.log"
    CustomLog "logs/yii2.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "D:/wamp/www/yiiadv/frontend/web"
    <Directory "D:/wamp/www/yiiadv/frontend/web">
        RewriteEngine on
        Allow from all
        # If a directory or a file exists, use the request directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Otherwise forward the request to index.php
        RewriteRule . index.php
    </Directory>
    ServerName www.yiiadv.com
    ServerAlias www.yiiadv.com
    ErrorLog "logs/yiiadv.com-error.log"
    CustomLog "logs/yiiadv.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "D:/wamp/www/yiiadv/backend/web"
    <Directory "D:/wamp/www/yiiadv/backend/web">
        RewriteEngine on
        Allow from all
        # If a directory or a file exists, use the request directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Otherwise forward the request to index.php
        RewriteRule . index.php
    </Directory>
    ServerName www.backend.yiiadv.com
    ErrorLog "logs/yiiadv.com-error.log"
    CustomLog "logs/yiiadv.com-access.log" common
</VirtualHost>

Apache中没有捕获到的主机名,默认使用第一个虚拟主机。
问题描述不明不白..
确认你的配置已生效,
确定各端口都正常访问,
再确认没有别的规则指向www目录

(新手求助)AR接收不到表单数据[2.0]

richpony阅读(77)

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use frontend\models\Post;
$this->title = '发帖';
    $this->params['breadcrumbs'][] = $this->title;
<?php $form=ActiveForm::begin([
                'id' => 'post-form',
                'options' => ['class' => 'class_name'],
                'action'=>'index.php?r=index/post',
                'method'=>'post',
            ]); 
?>
<?= $form->field($model,'title')->textInput()->label('标题');?>

            <?= $form->field($model,'content')->textarea(['rows'=>6,'id'=>'editor','class'=>'col-sm-1 col-md-12'])->label('文章');?>

            <div class="form-group">
            <?=  Html::submitButton('提交', [
                'class'=>'btn btn-primary',
                'name' =>'submit-button'])?>
            </div>

            <?php ActiveForm::end();?>

view

namespace frontend\controllers;

use yii;
use frontend\models\Post;
use yii\web\Controller;

class IndexController extends Controller{
    public function actionIndex(){

//        $model =new Post();
        return $this->render('post',[
                'model' => new Post()
            ]);
    }
    public function actionPost(){

        $model =new Post();

        if(yii::$app->request->post())
        {
            $model->title=yii::$app->request->post('title');
            $model->content=yii::$app->request->post('content');
            $model->save();
            return $this->render('post_success',[
                'model'=>$model
            ]);
        }
        else
        {
            return $this->render('post',[
                'model'=>$model
            ]);
        }
    }
}

controller

namespace frontend\models;

use Yii;


class Post extends \yii\db\ActiveRecord
{

    public $title;
    public $content;

    public static function tableName()
    {
        return 'post';
    }

    public function rules()
    {
        return [
            [['title'], 'required','message' => '标题不能为空.'],
            [['content'],'required','message' => '文章内容不能为空.'],
            [['title'], 'string', 'max' => 50],
            [['content'], 'string', 'max' => 200],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'content' => 'Content',
        ];
    }
}

model

刚接触YII的一个新手

你要看一下 HTML 上的 ActiveForm
你取用的方式是 name = “xxxx”
我記得ActiveForm 是已陣列方式呈現 name =”ModelName[……]”
如果不確定的話:
你大可直接 $_POST 看看傳入值是長甚麼樣子
楼主应该打开firebug,查看一下网络传输参数,一看便知。

yii2中migrate注释、索引[2.0]

HazelJames阅读(90)

在yii2中写迁移脚本,怎么添加普通索引,怎么写注释。

迁移脚本中不为空可以用not_null()这个方法,默认值有defaultValue()

那么普通索引以及注释呢?

非常感谢大家的回复,下面是我实现的方式,献丑了、、、

$this->createTable(self::TBL_NAME, [
            'id' => Schema::TYPE_PK,
            'user_id' => Schema::TYPE_INTEGER . ' NOT NULL COMMENT "用户ID"',
            'path' => Schema::TYPE_STRING . ' NOT NULL COMMENT "路径"',
            'created_at' => Schema::TYPE_INTEGER . ' NOT NULL COMMENT "上传时间"'
        ], $tableOptions);

普通索引用$this->index()
参考yii自带的migrate文件
你所需要的comment函数,将在不久的将来支持。
Comment methods for schema builder

请教个事,yiirestful接口提供的四个方法get,delete,put[2.0]

AniLion阅读(85)

在postman插件中使用是正常的,但是使用post创建新的用户的时候传参数一直为空,查看了数据库插入了一条数据,不过数据的用户名和密码为空,更新时间什么的就不为空,再次执行就报错了,请问什么原因
代码是这样的:

<?php
/**
 * Created by PhpStorm.
 * Date: 11/13/15
 * Time: 5:25 PM
 */

namespace api\controllers;


use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'common\models\User';

}

Body
x-www-form-urlencoded

yii子模块如何调用父模块的动作[2.0]

NeoKeo阅读(83)

如题,不在同一个module里,怎么调用其他module的动作action。或者在frontend里有一个module,在这个子module怎么调用frontend的动作呢?谢谢。

比如,在子模块里,想$this->redirect(['site/index'])总是失败的,一般跳到子模块下的site/index,加上frontend/site/index却无法访问。

frontend/site/index 前面加上 / 即可:
Guide Handling Requests – Routing and URL Creation – Creating URLs 里面讲到:
你可以读读这节内容,了解一下 Yii 将 route 转换成 url 的过程。

登录

找回密码

注册