swoft 學習之 踩坑筆記

1. Redis 寫入數組

1.1 不開啓自動序列化–> 讀到的是一個字符串(“array”)

		vdump('0',$device);
		$result = $redis->set($tree, $device);
		vdump($device->get($tree));
		CALL ON App\Tcp\Redis\RedisDeviceTree(74):
		string(1) "0"
		array(1) {
		  ["Y0028115200001"]=> array(2) {
		    ["fd"]=> int(1)
		    ["id"]=> int(20190624)
		  }
		}
		CALL ON App\Tcp\Redis\RedisDeviceTree(76):
		string(5) "Array"

1.2 開啓序列化之後–> 讀到的是一個數組

	    'redisTcp' => [
	        'class'    => RedisDb::class,
	        'host'     => 'redis',
	        'port'     => 6379,
	        'database' => 0,
	        'option'   => [
	            'prefix'        => 'tcp:',
	            'serializer'    => Redis::SERIALIZER_PHP,	//開啓序列化
	        ]
	    ],
        vdump('0',$device);
        $result = $redis->set($tree, $device);
        vdump($device->get($tree));
		Redis 存的內容
		127.0.0.1:16379> get tcp:deviceTree
		"a:1:{s:14:\"Y0028115200001\";a:2:{s:2:\"fd\";i:1;s:2:\"id\";i:20190624;}}"

		CALL ON App\Tcp\Redis\RedisDeviceTree(74):
		string(1) "0"
		array(1) {
		  ["Y0028115200001"]=> array(2) {
		    ["fd"]=> int(1)
		    ["id"]=> int(20190624)
		  }
		}
		CALL ON App\Tcp\Redis\RedisDeviceTree(76):
		array(1) {
		  ["Y0028115200001"]=> array(2) {
		    ["fd"]=> int(1)
		    ["id"]=> int(20190624)
		  }
		}

2.TCP 服務器中監聽HTTP 做API 接口

2.1 常規思維是


    'tcpServer'         => [
        'port'  => 18309,
        'debug' => 1,
        'setting' => [
        ],
        'listener' => [
            'http' => bean('httpServer'),
        ],
    ],

2.2實際是

    'httpServer'        => [
        'class'    => HttpServer::class,
        'port'     => 18306,
        'listener' => [
            'tcp' => bean('tcpServer'),
        ],
        'process'  => [
        ],
        'on'       => [
        ],
        'setting' => [
        ]
    ],

原因:這其實並不算swoft 的坑,應該算是swoole 的坑,swoole 再listenerPort後,不管是主端口還是副端口有事件產生的時候,都會響應到主端口監聽的事件中,此時就會導致一個異常,http 本身就是TCP 協議的擴展,所以會導致自身的協議異常,但是反過來,由於自定義的協議和http 衝突的概率很小,所以底層就直接把 這部分數據包給過濾了

3.swoft 數據庫的讀寫

swoft 的數據庫框架是基於laravel 的,所以大部分可以參考laravel 文檔

3.1.數據庫查詢

3.1.1 簡單查詢

3.1.1.1 通過主鍵id 查詢

$recode = User::find($id)->first();					//獲取第一個
$recode = User::find($id)->get()->limit(10);		//獲取前十個數據

3.1.1.2 通過字段名 查詢

$recode = User::where('facId',  $id)->first();
$recode = User::where('facId',  $id)->get();		//獲取所有的

3.1.2聯合查詢 joins

$db = dbHj212Data::join('device_info', 'device_info.id', '=', 'data.id_device_info')
                ->where('device_info.facId', $id)
                ->get(['insert_time', 'data']);

3.1.1 簡單查詢

兩種方式
a.forPage 查詢
返回的結果爲 一個Builder對象,需要使用get 獲獲取實際數據,另外受影響行數需要另外統計

$db = dbHj212Data::join('device_info', 'device_info.id', '=', 'data.id_device_info')
                ->where('device_info.facId', $id)
                ->forPage($curPage, $perPage)
                ->get(['insert_time', 'data']);

返回

[
    [0]=> array(2) {
      ["insertTime"]=> int(2019)
      ["data"]=> array([])
    }
]

b.paginate 查詢
返回的結果爲 一個數組,已包括總行數(“count”)

$db = dbHj212Data::join('device_info', 'device_info.id', '=', 'data.id_device_info')
                ->where('device_info.facId', $id)
                ->paginate($curPage, $perPage, ['insertTime', 'data']);

返回

 [
  ["count"]=> int(2896)
  ["list"]=> array(1) {
    [0]=> array(2) {
      ["insertTime"]=> int(2019)
      ["data"]=> array([])
    }
  }
]

4.新建對象

4.1 添加 new() 方法

	首先 繼承 PrototypeTrait 對象( use PrototypeTrait;)
	然後 foreach $param 中的key=> value, 檢查是否存在於當前對象
	再進行賦值

    public static function new(array $param): self
    {
        $self        = self::__instance();

        foreach ($param as $attribute => $value)
        {
            if(property_exists($self, $attribute))
            {
                $self->$attribute = $value;
            }
        }

        return $self;
    }
另外一種方法(源代碼中的)
    public static function new(array $attributes = []): self
    {
        try {
            /* @var static $self */
            $self = bean(Proxy::getClassName(static::class));
        } catch (Throwable $e) {
            throw new DbException($e->getMessage());
        }

        $self->syncOriginal();
        $self->fill($attributes);
        $self->swoftExists = false;

        return $self;
    }

4.2 使用 new 方法


        dbHj212MinuteData::new([
            'deviceInfo' => $deviceInfo->getId(),
            'data'          => $request->getPackage()->getDataString(),
            'package'       => $request->getRawData()
        ])->save();
這個時候需要注意一件事,你如果使用第一種方法建立了new 方法,那這樣直接調用是沒有問題的
如果採用了第二種方法,那就要注意你的屬性名稱是否 有定義別名(prop="deviceInfo"),如果有,需要key 改成別名
    /**
     * @Column(name="id_device_info", prop="deviceInfo")
     *
     * @var int|null
     */
    private $idDeviceInfo;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章